Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (5.3k points)

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?

1 Answer

0 votes
by (119k points)
edited by

For precise measurements of elapsed time, you can use System.nanoTime(). System.currentTimeMills() provides you the nanosecond precision possible elapsed time in milliseconds since the epoch, but System.nanoTime() provides you a nanosecond-precise elapse time, relative to some arbitrary point.

If you want to measure the time the code takes to execute:

long startTime = System.nanoTime();    

// ... the code being measured ...    

long estimatedTime = System.nanoTime() - startTime;

If you want to learn Java, you can take up this Java Certification by Intellipaat.

Related questions

0 votes
1 answer
asked Mar 16, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Mar 4, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...