Take a look at the following code in Java:
long l = System.currentTimeMillis();
int[] arr = new int[2];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < 600000000; j++) {
arr[i] += 1;
}
}
System.out.println("done " + (System.currentTimeMillis() - l));
It takes about 3.5 seconds on my (dual core) machine.
If I add line
int u = 0;
at the beginning (before long l declaration) it takes 6.5 seconds!
If I now add additional line so that I have:
int u = 0;
int u2 = 0;
we are back to 3.5 seconds.
With
int u = 0;
int u2 = 0;
int u3 = 0;
6.5 seconds again!
and with 4 is 3.5 sec, and with 5 and more 3.6 constantly.
Does this happen on someone else's machine? What's happening here?
To see if JIT or JVM startup have any effect, I wrapped whole thing in a loop of 100 iterations, and still results are the same