我认为答案是垃圾收集器正在运行并改变你的时间。
免责声明:我看不到 OP 代码的整个上下文,因为您没有发布可编译的示例;我假设您正在重新分配数组而不是重用它。如果不是,那么这不是正确的答案!
考虑这段代码:
using System;
using System.Diagnostics;
namespace Demo
{
internal class Program
{
private static void Main(string[] args)
{
var ar = new int[500000000];
test1(ar);
//ar = new int[500000000]; // Uncomment this line.
test2(ar);
}
private static void test1(int[] ar)
{
var sw = new Stopwatch();
sw.Start();
var length = ar.Length;
for (var i = 0; i < length; i++)
{
if (ar[i] == 0);
}
sw.Stop();
Console.WriteLine("test1 took " + sw.Elapsed);
}
private static void test2(int[] ar)
{
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < ar.Length; i++)
{
if (ar[i] == 0);
}
sw.Stop();
Console.WriteLine("test2 took " + sw.Elapsed);
}
}
}
在我的系统上打印:
test1 took 00:00:00.6643788
test2 took 00:00:00.3516378
如果我取消注释标记的行,// Uncomment this line.
则时间更改为:
test1 took 00:00:00.6615819
test2 took 00:00:00.6806489
这是因为 GC 收集了先前的数组。
[编辑] 为了避免 JIT 启动成本,我将整个测试放入一个循环中:
for (int i = 0; i < 8; ++i)
{
test1(ar);
ar = new int[500000000]; // Uncomment this line.
test2(ar);
}
然后我注释掉第二个数组分配的结果是:
test1 took 00:00:00.6437912
test2 took 00:00:00.3534027
test1 took 00:00:00.3401437
test2 took 00:00:00.3486296
test1 took 00:00:00.3470775
test2 took 00:00:00.3675475
test1 took 00:00:00.3501221
test2 took 00:00:00.3549338
test1 took 00:00:00.3427057
test2 took 00:00:00.3574063
test1 took 00:00:00.3566458
test2 took 00:00:00.3462722
test1 took 00:00:00.3430952
test2 took 00:00:00.3464017
test1 took 00:00:00.3449196
test2 took 00:00:00.3438316
并启用第二个数组分配:
test1 took 00:00:00.6572665
test2 took 00:00:00.6565778
test1 took 00:00:00.3576911
test2 took 00:00:00.6910897
test1 took 00:00:00.3464013
test2 took 00:00:00.6638542
test1 took 00:00:00.3548638
test2 took 00:00:00.6897472
test1 took 00:00:00.4464020
test2 took 00:00:00.7739877
test1 took 00:00:00.3835624
test2 took 00:00:00.8432918
test1 took 00:00:00.3496910
test2 took 00:00:00.6471341
test1 took 00:00:00.3486505
test2 took 00:00:00.6527160
请注意,由于 GC,test2 始终需要更长的时间。
不幸的是,GC 使计时结果变得毫无意义。
例如,如果我将测试代码更改为:
for (int i = 0; i < 8; ++i)
{
var ar = new int[500000000];
GC.Collect();
test1(ar);
//ar = new int[500000000]; // Uncomment this line.
test2(ar);
}
随着该行被注释掉,我得到:
test1 took 00:00:00.6354278
test2 took 00:00:00.3464486
test1 took 00:00:00.6672933
test2 took 00:00:00.3413958
test1 took 00:00:00.6724916
test2 took 00:00:00.3530412
test1 took 00:00:00.6606178
test2 took 00:00:00.3413083
test1 took 00:00:00.6439316
test2 took 00:00:00.3404499
test1 took 00:00:00.6559153
test2 took 00:00:00.3413563
test1 took 00:00:00.6955377
test2 took 00:00:00.3364670
test1 took 00:00:00.6580798
test2 took 00:00:00.3378203
并且没有注释:
test1 took 00:00:00.6340203
test2 took 00:00:00.6276153
test1 took 00:00:00.6813719
test2 took 00:00:00.6264782
test1 took 00:00:00.6927222
test2 took 00:00:00.6269447
test1 took 00:00:00.7010559
test2 took 00:00:00.6262000
test1 took 00:00:00.6975080
test2 took 00:00:00.6457846
test1 took 00:00:00.6796235
test2 took 00:00:00.6341214
test1 took 00:00:00.6823508
test2 took 00:00:00.6455403
test1 took 00:00:00.6856985
test2 took 00:00:00.6430923
我认为这个测试的寓意是:与其他代码相比,这个特定测试的 GC 开销如此之大,以至于它完全扭曲了计时结果,并且不能相信它们意味着什么。