System.currentTimeMillis vs System.nanoTime

后端 未结 10 1235
后悔当初
后悔当初 2020-11-21 22:56

Accuracy Vs. Precision

What I would like to know is whether I should use System.currentTimeMillis() or System.nanoTime()<

相关标签:
10条回答
  • 2020-11-21 23:03

    As others have said, currentTimeMillis is clock time, which changes due to daylight saving time, users changing the time settings, leap seconds, and internet time sync. If your app depends on monotonically increasing elapsed time values, you might prefer nanoTime instead.

    You might think that the players won't be fiddling with the time settings during game play, and maybe you'd be right. But don't underestimate the disruption due to internet time sync, or perhaps remote desktop users. The nanoTime API is immune to this kind of disruption.

    If you want to use clock time, but avoid discontinuities due to internet time sync, you might consider an NTP client such as Meinberg, which "tunes" the clock rate to zero it in, instead of just resetting the clock periodically.

    I speak from personal experience. In a weather application that I developed, I was getting randomly occurring wind speed spikes. It took a while for me to realize that my timebase was being disrupted by the behavior of clock time on a typical PC. All my problems disappeared when I started using nanoTime. Consistency (monotonicity) was more important to my application than raw precision or absolute accuracy.

    0 讨论(0)
  • 2020-11-21 23:04

    System.nanoTime() isn't supported in older JVMs. If that is a concern, stick with currentTimeMillis

    Regarding accuracy, you are almost correct. On SOME Windows machines, currentTimeMillis() has a resolution of about 10ms (not 50ms). I'm not sure why, but some Windows machines are just as accurate as Linux machines.

    I have used GAGETimer in the past with moderate success.

    0 讨论(0)
  • 2020-11-21 23:06

    If you're just looking for extremely precise measurements of elapsed time, use System.nanoTime(). System.currentTimeMillis() will give you the most accurate possible elapsed time in milliseconds since the epoch, but System.nanoTime() gives you a nanosecond-precise time, relative to some arbitrary point.

    From the Java Documentation:

    public static long nanoTime()
    

    Returns the current value of the most precise available system timer, in nanoseconds.

    This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

    For example, to measure how long some code takes to execute:

    long startTime = System.nanoTime();    
    // ... the code being measured ...    
    long estimatedTime = System.nanoTime() - startTime;
    

    See also: JavaDoc System.nanoTime() and JavaDoc System.currentTimeMillis() for more info.

    0 讨论(0)
  • 2020-11-21 23:10

    System.currentTimeMillis() is not safe for elapsed time because this method is sensitive to the system realtime clock changes of the system. You should use System.nanoTime. Please refer to Java System help:

    About nanoTime method:

    .. This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis()..

    If you use System.currentTimeMillis() your elapsed time can be negative (Back <-- to the future)

    0 讨论(0)
  • 2020-11-21 23:11

    For game graphics & smooth position updates, use System.nanoTime() rather than System.currentTimeMillis(). I switched from currentTimeMillis() to nanoTime() in a game and got a major visual improvement in smoothness of motion.

    While one millisecond may seem as though it should already be precise, visually it is not. The factors nanoTime() can improve include:

    • accurate pixel positioning below wall-clock resolution
    • ability to anti-alias between pixels, if you want
    • Windows wall-clock inaccuracy
    • clock jitter (inconsistency of when wall-clock actually ticks forward)

    As other answers suggest, nanoTime does have a performance cost if called repeatedly -- it would be best to call it just once per frame, and use the same value to calculate the entire frame.

    0 讨论(0)
  • 2020-11-21 23:15

    one thing here is the inconsistency of the nanoTime method.it does not give very consistent values for the same input.currentTimeMillis does much better in terms of performance and consistency,and also ,though not as precise as nanoTime,has a lower margin of error,and therefore more accuracy in its value. i would therefore suggest that you use currentTimeMillis

    0 讨论(0)
提交回复
热议问题