在尝试选择推荐哪种索引方法时,我尝试测量性能。然而,测量结果让我很困惑。我以不同的顺序运行了多次,但测量结果保持一致。以下是我衡量性能的方法:
for N = [10000 15000 100000 150000]
x = round(rand(N,1)*5)-2;
idx1 = x~=0;
idx2 = abs(x)>0;
tic
for t = 1:5000
idx1 = x~=0;
end
toc
tic
for t = 1:5000
idx2 = abs(x)>0;
end
toc
end
这是结果:
Elapsed time is 0.203504 seconds.
Elapsed time is 0.230439 seconds.
Elapsed time is 0.319840 seconds.
Elapsed time is 0.352562 seconds.
Elapsed time is 2.118108 seconds. % This is the strange part
Elapsed time is 0.434818 seconds.
Elapsed time is 0.508882 seconds.
Elapsed time is 0.550144 seconds.
我检查了大约 100000 的值,这也会发生,即使在 50000 时也会发生奇怪的测量。
所以我的问题是:有没有其他人在一定范围内经历过这种情况,是什么原因造成的?(这是一个错误吗?)