C#循环效率比较 Item6 - Prefer 'for' over 'foreach'. [Performance]
2011-08-22 20:04
513 查看
Item6 - Prefer 'for' over 'foreach'. [Performance]
The main differences in using the foreach statement are that you do not need to deal with increments and with the end of the loop expression. Moreover, the foreach statement is designed to traverse through the entire collection.
One can say that foreach is a private case of for.
In the code snippets below, we can see that both loop blocks produce the same results, only under the hood the foreach hurts the performance. More variables are involved and additional heavy array copy.
The foreach is far more handier to use especially for collections but if your code runs over large collections, prefer using 'for'.
//foreach
Stopwatch watch = new Stopwatch();
watch.Start();
int[] arrayOfInts = new int[50000000];
int sum = 0;
foreach (int i in arrayOfInts) {
sum += i;
}
watch.Stop();
System.Console.WriteLine(watch.ElapsedMilliseconds + "ms"); //431ms
watch.Reset();
watch.Start();
//for
sum = 0;
for (int i = 0; i < arrayOfInts.Length; i++) {
sum += arrayOfInts[i];
}
watch.Stop();
System.Console.WriteLine(watch.ElapsedMilliseconds + "ms"); //313ms
The main differences in using the foreach statement are that you do not need to deal with increments and with the end of the loop expression. Moreover, the foreach statement is designed to traverse through the entire collection.
One can say that foreach is a private case of for.
In the code snippets below, we can see that both loop blocks produce the same results, only under the hood the foreach hurts the performance. More variables are involved and additional heavy array copy.
The foreach is far more handier to use especially for collections but if your code runs over large collections, prefer using 'for'.
//foreach
Stopwatch watch = new Stopwatch();
watch.Start();
int[] arrayOfInts = new int[50000000];
int sum = 0;
foreach (int i in arrayOfInts) {
sum += i;
}
watch.Stop();
System.Console.WriteLine(watch.ElapsedMilliseconds + "ms"); //431ms
watch.Reset();
watch.Start();
//for
sum = 0;
for (int i = 0; i < arrayOfInts.Length; i++) {
sum += arrayOfInts[i];
}
watch.Stop();
System.Console.WriteLine(watch.ElapsedMilliseconds + "ms"); //313ms
相关文章推荐
- C#中 for与foreach的效率比较
- for、foreach和MoveNext循环效率粗比较
- c# for foreach执行效率比较
- c# foreach 和for循环比较
- Android Error retrieving parent for item: No resource found that matches the given name '...'
- Java for循环和foreach循环的性能比较
- foreach与正常for循环效率对比。
- C#中for和foreach循环的性能
- 这是网友发的一道很常见的面试题, for(foo('A');foo('B')&&(i<2);foo('C')) ,其实考的就是for (初始化语句; 判断条件; 循环条件) ,现在详细解析一下!
- C# 中 for和foreach 性能比较,提高编程性能
- C#中循环语句:while、for、foreach的使用
- for-forEach-stream三种遍历方法执行效率比较与选用思考
- C#循环while、do...while、for、foreach
- for 循环性能比较 提高for循环的效率
- error: Error retrieving parent for item: No resource found that matches the given name 'android:Them
- for和foreach的效率比较
- error: Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCo
- for循环和递归算法的运行效率比较(c语言)
- 这是网友发的一道很常见的面试题, for(foo('A');foo('B')&&(i<2);foo('C')) ,其实考的就是for (初始化语句; 判断条件; 循环条件) ,现在详细解析一下!
- Error retrieving parent for item: No resource found that matches the given name 'android:Theme....