Connect the dots

“Progress doesn't come from early risers – progress is made by lazy men looking for easier ways to do things.” - Robert Heinlein

导航

C# 中的性能问题

Posted on 2005-04-06 23:16  TOX  阅读(249)  评论(0编辑  收藏  举报
源代码
   object[] objs1 = new object[10];
   foreach(object obj in objs1)
   {
    Console.WriteLine(obj.ToString());
   }

编译后的代码
     object[] objArray2 = objArray1;
      for (int num2 = 0; num2 < objArray2.Length; num2++)
      {
            object obj1 = objArray2[num2];
            Console.WriteLine(obj1.ToString());
      }

源代码
   object[] objs1 = new object[10];
   for (int index = 0; index < objs1.Length; ++index)
   {   
    Console.WriteLine(objs1[index].ToString());
   }

编译后的代码
     object[] objArray2 = objArray1;
      for (int num1 = 0; num1 < objArray1.Length; num1++)
      {
            Console.WriteLine(objArray1[num1].ToString());
      }

也就是说 foreach 会额外的产生一些代码,效率没有直接用 for 循环来的高