






















看了 冰戈的文章,根据他的我也测试了一下,结果跟他差不多,但是在无意中发现,下面的代码执行结果就出人意料,目前我还不知道是原因导致这样的结果,代码如下:
private static void TestForForeach(int count)
{
double[] test=new double[count];
for(int i=0;i<count;i++)
{
test[i]=i;
}
double sum1=0;
DateTime start=DateTime.Now;
foreach(double k in test)
{
sum1+=k;
}
DateTime end=DateTime.Now;
Console.WriteLine("TestForForeach2->\nForeach:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
sum1=0.0;
start=DateTime.Now;
for(int i=0;i<count;i++)
{
sum1+=test[i];
}
end=DateTime.Now;
Console.WriteLine("TestForForeach2->\nFor:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
}
private static void TestForForeach1(int count)
{
double[] test=new double[count];
for(int i=0;i<count;i++)
{
test[i]=i;
}
double sum1=0;
DateTime start=DateTime.Now;
for(int i=0;i<count;i++)
{
sum1+=test[i];
}
DateTime end=DateTime.Now;
Console.WriteLine("TestForForeach1->\nFor:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
sum1=0.0;
start=DateTime.Now;
foreach(double k in test)
{
sum1+=k;
}
end=DateTime.Now;
Console.WriteLine("TestForForeach1->\nForeach:开始时间{0},结束时间{1},\n相差:{2}",start,end,end-start);
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。