I just found System.currentTimeMillis is not accurate on windows XP, now I tried System.nanoTime()
with the same code.
Since 1ms = 1,000,000ns
, so I think the result should be 15,000,000ns
, but it's not.
See the sample code:
public class NanoTime {
public static void main(String[] args) {
long start = 0;
long end = 0;
while (true) {
if (start == 0) {
start = System.nanoTime();
} else {
long current = System.nanoTime();
if (current != start) {
end = current;
break;
}
}
}
System.out.println("The time interval of your OS: " + (end - start) + "ns");
}
}
The result is:
The time interval of your OS: 655ns
Seems it's much better than System.currentTimeMillis()
. But why? Can we believe this result?