Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the arbitrary point that is referenced in System.nanoTime Javadoc?

Tags:

java

time

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

  1. Is this point in time constant across JVM instances on the same platform?
  2. Is this point in time constant across JVM instances across platforms?

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

like image 798
Sirish Renukumar Avatar asked Dec 15 '22 06:12

Sirish Renukumar


1 Answers

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.

like image 186
Andy Turner Avatar answered Dec 17 '22 01:12

Andy Turner