Possible Duplicate:
Java: how much time does an empty loop use?
Take a look at the following:
for (int j = 0 ; j < 5 ; j++) {
long t1 = System.currentTimeMillis();
for (int i = 0 ; i < 10000000 ; i++)
; // intentionally empty
long t2 = System.currentTimeMillis();
System.out.println (t2 - t1);
}
Output (one of many possible):
2
11
0
0
0
If you run this program multiple times, you will find that the first two numbers of the output are always non-zero while all of the other numbers are indeed 0. Furthermore, the second number usually seems to be higher than the first. This appears to hold even if we loop higher, say until j < 100
. Is there any specific reason for this? Of course, milliseconds probably wouldn't make much of a difference in general but I'm just curious.