Why System.nanoTime () takes 4400 seconds nano

I tested some algorithms in which I was surrounded by a nanosecond timer, when I accidentally forgot to delete 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. This will be 4.4 microseconds, while this code is:

    a = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++)
        System.nanoTime();
    System.out.println(System.currentTimeMillis() - a);

Fingerprints 0

+5
source share
2 answers

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 is a difference between the two timers used: it currentTimeMilliscan be adjusted to skew the clock, but nanoTimeit can’t, but I doubt that the game is here.

+4
source

nanoseconds() , , , . , "" , "", "" - - , 0,1 .

, , - nanoseconds() - , .

0

All Articles