Accuracy Vs. Precision
What I would like to know is whether I should use System.currentTimeMillis() or System.nanoTime() when updating my object's positions in my game? Their change in movement is directly proportional to the elapsed time since the last call and I want to be as precise as possible.
I've read that there are some serious time-resolution issues between different operating systems (namely that Mac / Linux have an almost 1 ms resolution while Windows has a 50ms resolution??). I'm primarly running my apps on windows and 50ms resolution seems pretty inaccurate.
Are there better options than the two I listed?
Any suggestions / comments?
I've had good experience with nanotime. It provides wall-clock time as two longs (seconds since the epoch and nanoseconds within that second), using a JNI library. It's available with the JNI part precompiled for both Windows and Linux.
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
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.
Yes, if such precision is required use
System.nanoTime()
, but be aware that you are then requiring a Java 5+ JVM.On my XP systems, I see system time reported to at least
100 microseconds278 nanoseconds using the following code: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, butSystem.nanoTime()
gives you a nanosecond-precise time, relative to some arbitrary point.From the Java Documentation:
For example, to measure how long some code takes to execute:
See also: JavaDoc System.nanoTime() and JavaDoc System.currentTimeMillis() for more info.
System.currentTimeMillis()
is not safe for elapsed time because this method is sensitive to the system realtime clock changes of the system. You should useSystem.nanoTime
. Please refer to Java System help:About nanoTime method:
If you use
System.currentTimeMillis()
your elapsed time can be negative (Back <-- to the future)