I was testing some algorithms which I surrounded with a nanoseconds timer when I randomly forgot to remove the timer I found out that this code:
a = System.nanoTime();
System.out.println(System.nanoTime() - a);
always prints 4400 nano seconds on my system. That would be 4.4 microseconds whereas this code:
a = System.currentTimeMillis();
for (int i = 0; i < 1000; i++)
System.nanoTime();
System.out.println(System.currentTimeMillis() - a);
Prints 0
4400 nanoseconds is 4.4 microseconds, or 0.0044 milliseconds. The second example will always print zero because the elapsed time is much less than one millisecond. Then there are the differences between the two timers used: currentTimeMillis
can get adjusted for clock skew while nanoTime
cannot, but I doubt that's in play here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With