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?
Best Answer
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.