Precision vs. accuracy of System.nanoTime()

后端 未结 4 1975
星月不相逢
星月不相逢 2020-11-30 09:35

The documentation for System.nanoTime() says the following (emphasis mine).

This method can only be used to measure elapsed time and is not related to

4条回答
  •  旧巷少年郎
    2020-11-30 10:34

    The first interpretation is correct. On most systems the three least-significant digits will always be zero. This in effect gives microsecond accuracy, but reports it at the fixed precision level of a nanosecond.

    In fact, now that I look at it again, your second interpretation is also a valid description of what is going on, maybe even more so. Imagining freezed time, the report will be always the same wrong number of nanoseconds, but correct if understood as the integer number of microseconds.

提交回复
热议问题