I\'m trying to use following code with System.nanoTime()
to measure the elapsed time of code.
public static void main(String[] args) throws Exce
The nanoTime can be taken of the CPU clock cycle counter. As different CPUs can be started at slightly different times, the clock counter can be different on different CPUs. Linux corrects for this, but older versions of Windows do not. (I assume you have two CPUs which were started 3 ms apart)
You should also see positive jumps of over 2.5 ms occasionally.
Try
if (cost < 0 || cost > 2000000) {
And you should see some jump forward and some jump backward as the process is switched between CPUs.