且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

System.nanoTime()的精度与准确度

更新时间:2023-02-26 18:57:24

第一种解释是正确的。在大多数系统中,三个最低有效数字将始终为零。这实际上提供了微秒精度,但是以纳秒的固定精度水平报告。

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.