0 votes
1 view
in Java by (5.4k 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 (66.4k 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
0 votes
1 answer
asked Oct 13, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 29, 2019 in Java by Shubham (3.9k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...