Точность против точности System.nanoTime ()

Документация дляSystem.nanoTime() говорит следующее (выделение мое).

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change.

На мой взгляд, это можно интерпретировать двумя разными способами:

The sentence in bold above refers to individual return values. Then, precision and accuracy are to be understood in the numerical sense. That is, precision refers to the number of significant digits - the position of truncation, and accuracy is whether the number is the correct one (such as described in the top answer here What is the difference between 'precision' and 'accuracy'? )

The sentence in bold above refers to the capability of the method itself. Then, precision and accuracy are to be understood as illustrated by the dartboard analogy ( http://en.wikipedia.org/wiki/Precision_vs._accuracy#Accuracy_versus_precision:_the_target_analogy ). So, low accuracy, high precision => the wrong value is repeatedly hit with a high precision: imagining that physical time stands still, consecutive calls of nanoTime() returns the same numerical value, but it is off from the actual elapsed time since the reference time by some constant offset.

Какая интерпретация правильная? Моя точка зрения, интерпретация 2 будет означать, что мера времениdifference использование nanoTime () (путем вычитания двух возвращаемых значений) будет верным для наносекунды (поскольку постоянная ошибка / смещение в измерении будет устранена), в то время как интерпретация 1 не будет гарантировать такого рода соответствия между измерениями и, следовательно, не будет Обязательно подразумевают высокую точность измерений разницы во времени.

Updated 4/15/13: Документация по Java 7 дляSystem.nanoTime() был обновлен для устранения возможной путаницы с предыдущей формулировкой.

Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). 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.

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis().

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

The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed.

Ответы на вопрос(4)

Ваш ответ на вопрос