Does Java JIT compiler sacrifice performance to favor Collections?

Something is very wrong here: When I run the array version, I get an average execution time of 20000 nanoseconds. It is downright impossible for my 2 GHz CPU to execute 500000 loop iterations in that time, as that would imply the average loop iteration to take 20000/500000 = 0.04 ns, or 0.08 cpu cpu cycles …

The main reason is a bug in your timing logic: In the array version, you do

int index = 0;

for every timing, hence

samples[index++] =(endNanos);

will always assign to first array element, leaving all others at their default value of 0. Hence when you take the average of the array, you get 1/1000 of the last sample, not the average of all samples.

Indeed, if you move the declaration of index outside the loop, no significant difference is reported between the two variants.

Leave a Comment