var arr = Enumerable.Range(0, 100000000).ToList(); var sw = Stopwatch.StartNew(); for(var i = 0;i < arr.Count; i++){} sw.Stop(); Console.WriteLine("for loop takes : " + sw.ElapsedTicks); sw = Stopwatch.StartNew(); foreach(var x in arr){} sw.Stop(); Console.WriteLine("for loop takes : " + sw.ElapsedTicks);
... IL_0018: ldc.i4.0 IL_0019: stloc.2 // i IL_001A: br.s IL_0022 IL_001C: nop IL_001D: nop IL_001E: ldloc.2 // i IL_001F: ldc.i4.1 IL_0020: add IL_0021: stloc.2 // i IL_0022: ldloc.2 // i IL_0023: ldloc.0 // arr IL_0024: callvirt System.Collections.Generic.List<System.Int32>.get_Count IL_0029: clt IL_002B: stloc.s 04 // CS$4$0000 IL_002D: ldloc.s 04 // CS$4$0000 IL_002F: brtrue.s IL_001C ...
... IL_005A: ldloc.0 // arr IL_005B: callvirt System.Collections.Generic.List<System.Int32>.GetEnumerator IL_0060: stloc.s 05 // CS$5$0001 IL_0062: br.s IL_006E IL_0064: ldloca.s 05 // CS$5$0001 IL_0066: call System.Collections.Generic.List<System.Int32>+Enumerator.get_Current IL_006B: stloc.3 // x IL_006C: nop IL_006D: nop IL_006E: ldloca.s 05 // CS$5$0001 IL_0070: call System.Collections.Generic.List<System.Int32>+Enumerator.MoveNext IL_0075: stloc.s 04 // CS$4$0000 IL_0077: ldloc.s 04 // CS$4$0000 IL_0079: brtrue.s IL_0064 IL_007B: leave.s IL_008C IL_007D: ldloca.s 05 // CS$5$0001 IL_007F: constrained. System.Collections.Generic.List<>.Enumerator IL_0085: callvirt System.IDisposable.Dispose IL_008A: nop IL_008B: endfinally IL_008C: nop ...
for loop takes : 764538 for loop takes : 1311252
原文:http://blog.csdn.net/lan_liang/article/details/50353241