Timer accuracy in java

前端 未结 2 618
野性不改
野性不改 2020-12-19 05:59

I\'m timing some things, which I can\'t just put in a long loop. And I need to time them to see how long they take to complete, but it seems like the timer has a 15-16 ms ac

2条回答
  •  轮回少年
    2020-12-19 06:09

    Clocks and Timers - General Overview

    Java Programming API's for Clocks and Timers The absolute "time-of-day" clock is represented by the System.currentTimeMillis() method, that returns a millisecond representation of wall-clock time in milliseconds since the epoch. As such it uses the operating system's "time of day" clock. The update resolution of this clock is often the same as the timer interrupt (eg. 10ms), but on some systems is fixed, independent of the interrupt rate.

    The relative-time clock is represented by the System.nanoTime() method that returns a "free-running" time in nanoseconds. This time is useful only for comparison with other nanoTime values. The nanoTime method uses the highest resolution clock available on the platform, and while its return value is in nanoseconds, the update resolution is typically only microseconds. However, on some systems there is no choice but to use the same clock source as for currentTimeMillis() - fortunately this is rare and mostly affects old Linux systems, and Windows 98.

提交回复
热议问题