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

对于数组的访问,是应该使用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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值