System.nanoTime()的精度与准确度 [英] Precision vs. accuracy of System.nanoTime()

查看:328
本文介绍了System.nanoTime()的精度与准确度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<$的文档c $ c> System.nanoTime() 说明以下内容(强调我的)。

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 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.

正如我所看到的,这可以用两种不同的方式解释:

As I see it, this can be interpreted in two different ways:


  1. 粗体中的句子以上是指个人的回报值。然后,从数值意义上理解精度和准确度。也就是说,精度是指有效数字的数量 - 截断的位置,准确度是指数字是否正确(如前面的答案中所述)
    '精度'和'准确度'之间有什么区别?

  1. 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'? )

上面粗体中的句子指的是方法本身的功能。然后,精确度和准确度将被理解为飞镖类比
http://en.wikipedia.org/wiki/Precision_vs._accuracy#Accuracy_versus_precision:_the_target_analogy )。
所以,低精度,高精度=>错误的值被高精度地重复击中:想象物理时间静止不动,nanoTime()的连续调用返回相同的数值,但它与实际值不同自参考时间以来经过的时间一些常数偏移。

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意味着使用nanoTime()(通过减去两个返回值)的时间差异的度量将是纳秒的正确(因为测量中的恒定误差/偏移将是虽然解释1不能保证测量之间的符合性,因此不一定意味着高精度的时差测量。

Which interpretation is the correct one? My point is, interpretation 2 would mean that a measure of time difference using nanoTime() (by subtracting two return values) would be correct to the nanosecond (since the constant error/offset in the measurement would be eliminated), while interpretation 1 wouldn't guarantee that kind of compliance between measurements and thus wouldn't necessarily imply a high precision of time difference measurements.

2013年4月15日更新: System.nanoTime() 已更新,以解决可能与之前的措辞。

Updated 4/15/13: The Java 7 documentation for System.nanoTime() has been updated to address the possible confusion with the previous wording.


返回正在运行的Java虚拟机的高分辨率时间源的当前值,以纳秒为单位。

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

此方法只能用于衡量已用时间,与其他任何时间无关系统或挂钟时间。返回的值表示纳秒,因为某些固定但任意的原点时间(可能在未来,因此值可能为负)。在Java虚拟机的实例中,此方法的所有调用都使用相同的原点;其他虚拟机实例可能使用不同的原点。

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.

此方法提供纳秒级精度,但不一定是纳秒级分辨率(即值的变化频率) - 否除了分辨率至少与 currentTimeMillis()

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().

连续调用的差异越大由于数值溢出,大约292年(2 63 纳秒)将无法正确计算经过的时间。

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

此方法返回的值变得有意义仅当计算在Java虚拟机的同一实例中获得的两个此类值之间的差异时。

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.


推荐答案

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

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.

这篇关于System.nanoTime()的精度与准确度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆