I understand that System.nanoTime()
has no relation to the epoch (which is related to System.currentTimeMillis()
though).
From the Javadoc it states that the value returned from System.nanoTime()
is an offset from some arbitrary point in time (may even be the future).
With regard to this
If suppose I run my application and persist the value of System.nanoTime()
into a file. Next I reexecute my application and compute the difference between the value in the file and the new value returned from System.nanoTime()
.
Then is it safe to assume that the difference is the actual amount of time between the executions?
It would be if the arbitrary point in time would remain the same.
However I am not sure is that's the behavior
No, it's arbitrary. Don't rely upon it being any given value, as it is not specified to work in that way. As it says in the Javadoc:
The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.
System.nanoTime()
is used to measure elapsed time on a single JVM, so you only care about the difference in its value between two invocations. Thus, knowing the offset isn't necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With