关于for和foreach,兼顾效率与安全

news/2025/2/27 5:18:09
对于数组的访问,是应该使用for的方式的,因为这样性能更高。以下代码是恰当的。
None.gif Object[] objArray  =  ...;
None.gif
int  objArrayLength  =  objArray.Length;
None.gif
for  ( int  i  =   0 ; i  <  objArrayLength;  ++ i)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// do something ...
ExpandedBlockEnd.gif
}

None.gif
None.gifString str 
=  ...;
None.gif
int  strLength  =  str.Length;
None.gif
for  ( int  i  =   0 ; i  <  strLength;  ++ i) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
// do something ...
ExpandedBlockEnd.gif
}

对ArrayList这样的可使用下标进行随机访问的数据结构,使用下标访问,要比foreach的方式进行顺序访问,速度要快一些。foreach这样写法,使用的过程产生一个额外的对象Enumerator,而且每次访问需要更多的操作,降低性能。下面的两种写法编译出的代码是一样的:
第一种写法:
None.gif IList list  =   new  ArrayList();
None.gifIEnumerator iter 
=  list.GetEnumerator();
None.gif
try
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
while (iter.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Object obj 
= iter.Current;
InBlock.gif        
//do something ...
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif
finally
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    IDisposable disposableObj 
= iter as IDisposable;
InBlock.gif    
if (disposableObj != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        disposableObj.Dispose();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

第二种写法:
None.gif IList list  =   new  ArrayList();
None.gif
foreach  (Object obj  in  list)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//do something ...
ExpandedBlockEnd.gif
}

对比这两种写法,第一种写法非常罗嗦,所以C#引入了foreach的语法。通过观察第一种写法,foreach是通过GetEnumerator获得一个IEnumerator对象,通过IEnumerator对象执行MoveNext()方法和获取Current属性进行遍历的。

我们再通过Reflector工具,查看mscorlib.dll中System.Collection.ArrayList的实现:
None.gif // 为了简单起见,我只列出ArrayList的Add、Clear、GetEnumerator的代码
None.gif
public   class  ArrayList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//这是一个版本标识,ArrayList对象,每做一个修改操作,_version都会加1
InBlock.gif
    private int _version;
InBlock.gif
InBlock.gif    
public virtual int Add(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int num1;
InBlock.gif        
if (this._size == this._items.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.EnsureCapacity((this._size + 1));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
this._items[this._size] = value;
InBlock.gif        
++this._version; //注意此处
InBlock.gif
        this._size = ((num1 = this._size) + 1);
InBlock.gif        
return num1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public virtual void Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Array.Clear(
this._items, 0this._size);
InBlock.gif        
this._size = 0;
InBlock.gif        
++this._version; //注意此处
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
//每次调用GetEnumerator方法,都会构造一个FastArrayListEnumerator
InBlock.gif    
//或者ArrayListEnumeratorSimple对象。
InBlock.gif
    public virtual IEnumerator GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (base.GetType() == typeof(ArrayList))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new ArrayList.FastArrayListEnumerator(this);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return new ArrayList.ArrayListEnumeratorSimple(this);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

通过上述代码可以看到,ArrayList是通过_version成员变量作版本标识的,每次执行Add、Clear等修改ArrayList内容的操作,都会将版本号加1,而每次调用GetEnumerator方法,都会构造一个FastArrayListEnumerator或者ArrayListEnumeratorSimple对象。我们再看FastArrayListEnumerator的实现:
None.gif class  FastArrayListEnumerator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private int version;
InBlock.gif
InBlock.gif    
internal FastArrayListEnumerator(ArrayList list)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.list = list;
InBlock.gif        
this.index = -1;
InBlock.gif
InBlock.gif        
//获取构建FastArrayListEnumerator对象时ArrayList的版本号
InBlock.gif
        this.version = list._version; 
InBlock.gif
InBlock.gif        
this.lastIndex = (list._size - 1);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public bool MoveNext()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int num1;
InBlock.gif
InBlock.gif        
//比较ArrayList当前的版本号,
InBlock.gif        
//是否和构建FastArrayListEnumerator对象时的版本号一致
InBlock.gif        
//如果不一致,则抛出异常。
InBlock.gif
        if (this.version != this.list._version)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new InvalidOperationException(
InBlock.gif                Environment.GetResourceString(
"InvalidOperation_EnumFailedVersion")
InBlock.gif                );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//... ... 
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

FastArrayListEnumerator对象构建时,当时时ArrayList的版本号。当执行MoveNext()操作时,检查ArrayList当前的版本号是否和FastArrayListEnumerator对象构建时的版本号一致,如果不一致就会抛出异常。

由于Enumerator中,做了版本检查处理的工作,所以使用foreach是线程安全,而使用for则不时。为什么呢?如果在使用foreach遍历对象的过程中,其他线程修改了List的内容,例如添加或者删除,就会出现不可知的错误,而使用foreach则能够正确抛出错误信息。

综上所述,结论如下:
使用for,更高效率。
使用foreach,更安全。

那么如何选择呢?我的建议是,在一些全局的,多线程可以访问的数据结构对象,使用foreach。而对本地变量,则使用for,效率和安全兼顾!例如:
None.gif public   void  F1(IList globalList)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    IList waitForDeleteList 
= new ArrayList();
InBlock.gif
InBlock.gif    
//全局变量,使用foreach,保证线程
InBlock.gif
    foreach (Object item in globalList)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
if (condition)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            waitForDeleteList.Add(item);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//本地变量使用for,保证效率
InBlock.gif
    int waitForDeleteListCount = waitForDeleteList.Count;
InBlock.gif    
for (int i = 0; i < waitForDeleteListCount; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        globalList.Remove(waitForDeleteList[i]);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

以上建议,对于在Java环境下也使用,我阅读过JDK 1.4的java.util.ArrayList的实现,.NET Framework的实现和JDK的实现,几乎是一样的,是否抄袭,见仁见智。上述的C#代码在Java环境中应为:
ExpandedBlockStart.gif ContractedBlock.gif public   void  f1(List globalList)  dot.gif {
InBlock.gif    List waitForDeleteList 
= new ArrayList();
InBlock.gif    
//全局变量,使用Iterator遍历,保证线程
InBlock.gif
    Iterator iter = globalList.iterator();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
while (iter.hasNext()) dot.gif{
InBlock.gif        Object item 
= iter.next();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (condition) dot.gif{
InBlock.gif            waitForDeleteList.add(item);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
//本地变量使用for,保证效率
InBlock.gif
    int waitForDeleteListCount = waitForDeleteList.size();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (int i = 0; i < waitForDeleteListCount; ++i) dot.gif{
InBlock.gif        globalList.remove(waitForDeleteList.
get(i));
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


注意,以上代码并不是做该项工作的最优算法,如果需要更高的效率,修改如下:
C#版本
None.gif public   void  F1(IList globalList)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
bool condition = true;
InBlock.gif    IList waitForDeleteList 
= new ArrayList();
InBlock.gif
InBlock.gif    
//全局变量,使用foreach,保证线程
InBlock.gif
    int itemIndex = 0;
InBlock.gif    
foreach (Object item in globalList)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (condition)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            waitForDeleteList.Add(index);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
++itemIndex;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//本地变量使用for,保证效率
InBlock.gif
    int waitForDeleteListCount = waitForDeleteList.Count;
InBlock.gif    
for (int i = waitForDeleteListCount - 1; i >= 0--i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        index 
= (int) waitForDeleteList[i];
InBlock.gif        globalList.RemoveAt(itemIndex);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

Java版本:
ExpandedBlockStart.gif ContractedBlock.gif public   void  f1(List globalList)  dot.gif {
InBlock.gif    List waitForDeleteList 
= new ArrayList();
InBlock.gif    
//全局变量,使用Iterator遍历,保证线程
InBlock.gif
    Iterator iter = globalList.iterator();
InBlock.gif    
int index = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
while (iter.hasNext()) dot.gif{
InBlock.gif        Object item 
= iter.next();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (condition) dot.gif{
InBlock.gif            waitForDeleteList.add(
new Integer(index));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
++index;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
//本地变量使用for,保证效率
InBlock.gif
    int waitForDeleteListCount = waitForDeleteList.size();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (int i = waitForDeleteListCount - 1; i >= 0--i) dot.gif{
InBlock.gif       index 
= ((Integer) waitForDeleteList.get(i)).intValue();
InBlock.gif        globalList.remove(index);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

http://www.niftyadmin.cn/n/3859987.html

相关文章

未分配利润与利润表不一致_未分配利润和净利润的关系

未分配利润和净利润的关系资产负债表中的“未分配利润”项目反映企业期末尚未分配的利润数额。而&#xff1a;期末未分配利润期初未分配利润本期实现的净利润-本期分配的利润(含提取得各项盈余公积、分配的股息红利、以利润转增资本等)。未分配利润和净利润的关系相关阅读未分配…

saltstack 之grains (一)

参考&#xff1a;http://www.cnblogs.com/shhnwangjian/p/5985868.html salt 的grains主要是存储静态的数据&#xff0c;主要是minion端的一些数据&#xff0c;比如&#xff0c;hostname&#xff0c;内存大小、IP&#xff0c;CPU等一些数据&#xff0c;主要是存储在minion端的。…

linux开放8082和3306端口

iptables -I INPUT -p tcp --dport8080 -j ACCEPT #开放8080端口(Tomcat)iptables -I INPUT -p tcp --dport3306 -j ACCEPT #开放3306端口(MySql)

职称认定评审考试

认定&#xff1a;认定表原件学历学位证书原件复印件身份证原件复印件&#xff12;寸照片&#xff11;认定费&#xff11;&#xff10;&#xff10;&#xff0c;证书&#xff11;&#xff10;转载于:https://www.cnblogs.com/applegirl/archive/2004/08/04/29962.html

js中正则表达式验证_前端开发中62条不可忽视的知识点总结

1.css禁用鼠标事件2.get/post的理解和他们之间的区别3.实现条纹网格的方式4.js求平面两点之间的距离5.css禁止用户选择6.数组去重7.什么是CDN和CDN的好处8.圣杯布局和双飞翼布局9.正则表达式匹配手机号10.如何提高首频加载速度11.浏览器内核(渲染引擎)12.浏览器渲染过程及优化建…

django forms使用

123456789101112131415161718192021222324252627282930313233343536373839404142434445在app目录下创建forms.py文件&#xff1a;from django import forms #导入相关模块class AddForm(forms.Form): #必须继承forms.Forma forms.EmailField() …

JavaScript基础知识巩固

JavaScript基础 输入输出语法 输出&#xff1a; document.write(要输出的内容) alert(页面弹出警告窗) console.log(控制台打印)输入&#xff1a; let value prompt(用户输入的内容)变量的本质 是程序在内存中申请的一块用来存放数据的空间变量命名规范 不能用关键字 关键…

mysql 创建删除数据库(linux)

1.Mysql 客户端创建、删除数据库&#xff1a; [rootlocalhost~]# /usr/local/mysql/bin/mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.1.60-log Source distribution Cop…