For a long time I measured the elapsed time with help System.currentTimeMillis()and only recently found out that I can also use nanoTime(). However, some of the results were not what I expected, as it nanoTimemeasures time from an arbitrary point in time instead of 01-01-1970how it does currentTimeMillis.
From JavaDoc tonanoTime :
Returns the current value of the most accurate system timer available, in nanoseconds.
This method can only be used to measure elapsed time and is not used associated with any other concept of system or wall time. The return value is nanoseconds , since it is a fixed but arbitrary time (possibly in the future, so the values can be negative). This method provides nanosecond accuracy, but not necessarily nanosecond accuracy. No guarantees are made regarding how often values change. Differences in consecutive calls that exceed approximately 292 years (263 nanoseconds) will not accurately calculate elapsed time due to numerical overflow.
For example, to determine how long it takes to execute code:
long startTime = System.nanoTime();
long estimatedTime = System.nanoTime() - startTime;
. , . API . , . , , , .
, nanoTime, . , , :
nanoTimenanoTime JVM (, JVM)nanoTime JVM
StackOverflow , , :
, , nanoTime , , , , , , .