for和foreach的执行效率的问题之新发现

看了 冰戈的文章,根据他的我也测试了一下,结果跟他差不多,但是在无意中发现,下面的代码执行结果就出人意料,目前我还不知道是原因导致这样的结果,代码如下:

None.gif          private   static   void   TestForForeach( int  count)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
double[] test=new double[count];
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                test[i]
=i;
ExpandedSubBlockEnd.gif            }

InBlock.gif        
InBlock.gif            
double sum1=0;
InBlock.gif
InBlock.gif            DateTime start
=DateTime.Now;
InBlock.gif            
foreach(double k in test)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sum1
+=k;
ExpandedSubBlockEnd.gif            }

InBlock.gif            DateTime end
=DateTime.Now;
InBlock.gif
InBlock.gif            Console.WriteLine(
"TestForForeach2->\nForeach:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
InBlock.gif
InBlock.gif        
InBlock.gif            sum1
=0.0;
InBlock.gif
InBlock.gif            start
=DateTime.Now;
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sum1
+=test[i];
ExpandedSubBlockEnd.gif            }

InBlock.gif            end
=DateTime.Now;
InBlock.gif
InBlock.gif            Console.WriteLine(
"TestForForeach2->\nFor:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
ExpandedBlockEnd.gif        }
上面这段代码是没有什么差别的。但是下面代码,结果就会出人意料了。
None.gif          private   static   void   TestForForeach1( int  count)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
double[] test=new double[count];
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                test[i]
=i;
ExpandedSubBlockEnd.gif            }

InBlock.gif        
InBlock.gif            
double sum1=0;
InBlock.gif
InBlock.gif            DateTime start
=DateTime.Now;
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sum1
+=test[i];
ExpandedSubBlockEnd.gif            }

InBlock.gif            DateTime end
=DateTime.Now;
InBlock.gif
InBlock.gif            Console.WriteLine(
"TestForForeach1->\nFor:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
InBlock.gif
InBlock.gif        
InBlock.gif            sum1
=0.0;
InBlock.gif
InBlock.gif            start
=DateTime.Now;
InBlock.gif            
foreach(double k in test)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sum1
+=k;
ExpandedSubBlockEnd.gif            }

InBlock.gif            end
=DateTime.Now;
InBlock.gif
InBlock.gif            Console.WriteLine(
"TestForForeach1->\nForeach:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
ExpandedBlockEnd.gif        }

这段代码的结果中,for与foreach的执行时间差不多,而且相对foreach还会快点,我查看了一下MSIL代码,结果是一样的,有谁知道原因的可以分析一下吗?

转载于:https://www.cnblogs.com/Oceanchip/archive/2005/06/29/183560.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值