For those who are interested in how I do the benchmark, look here , I simple replace / add a couple of methods near the build in "Loop 1K" method.
Sorry, I forgot to say my testing environment. .Net 4.5 x64 (don't pick 32bit preferred). in x86 both methods take 5x as much as time.
Loop2
takes 3x as much time as Loop
. I thought that x++
/ x+=y
should not slow down when x
gets larger (since it takes 1 or 2 cpu instructions any way)
Is it due to Locality of reference? However I thought that in Loop2
there are not many variables, they should all be close to each other...
public long Loop(long testSize)
{
long ret = 0;
for (long i = 0; i < testSize; i++)
{
long p = 0;
for (int j = 0; j < 1000; j++)
{
p+=10;
}
ret+=p;
}
return ret;
}
public long Loop2(long testSize)
{
long ret = 0;
for (long i = 0; i < testSize; i++)
{
for (int j = 0; j < 1000; j++)
{
ret+=10;
}
}
return ret;
}
Update: When, if ever, is loop unrolling still useful? is useful