Could someone explain this description of System.nanoTime()?

273 views Asked by At

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.

I'm not sure how the the values are not precise and logic behind the numerical overflow.

1

There are 1 answers

2
user2357112 On BEST ANSWER

This method provides nanosecond precision, but not necessarily nanosecond accuracy

The method will give you a number in nanoseconds, but the clock it uses may not actually be able to track nanoseconds. Some number of the last digits may be wrong. In the applications for which this method is used, this inaccuracy is fine.

Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

2^63 nanoseconds is too many nanoseconds for a long to represent. If you run your code for 292 years, so much time will have passed that Java is unable to express just how long it's been running.