Reputation: 531
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
Upvotes: 0
Views: 166
Reputation: 21993
Only explanation I have is data/cache alignment. Data is allready aligned to give good performance but adding other variables can shift things a bit giving higher or lower cache hit rates.
Personally I think it is more interesting to wonder why your system is taking seconds. Mine does that code in around 88ms! (Quad Core 2x2 2.66 GHz Mac Pro)
edit Found the reason, the 32bit JVM takes 2.5 seconds, the 64bit 88ms.
Upvotes: 0
Reputation: 14548
I dont think this is about java.
Maybe you have some other process running which uses your CPU at the time of execution.
You may wanna put another for loop that spans all other code and get an average time for like trying same thing 50 times.
as when I try your code, it always takes 3.4secs for all cases on my pc.
Upvotes: 0
Reputation: 4951
At first glance, I'd say not to worry about it - you're basically obsessing over the results of a microbenchmark. It's almost impossible to be deterministic; even if there's an actual performance differential, it's most likely because JIT is trying to figure out what the heck the extra variable is doing and if there's a side-effect.
Do a full warmup, and you'll get more deterministic results, but it's still a microbenchmark.
Upvotes: 1
Reputation: 72039
I get 2 seconds in all cases. You're probably getting different results due to something else. There are all sorts of factors that affect a micro-benchmark. See How do I write a correct micro-benchmark in Java?
Upvotes: 2