gpt4 book ai didi

java - System.nanoTime() 的精度与精度

转载 作者:IT老高 更新时间:2023-10-28 20:41:18 25 4
gpt4 key购买 nike

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.

在我看来,这可以用两种不同的方式来解释:

  1. 上面粗体中的句子是指单个返回值。然后,精度和准确度要从数字的意义上来理解。即精度是指有效数字的个数——截断的位置,而精度是指数字是否正确(如这里的置顶答案中描述的) What is the difference between 'precision' and 'accuracy'? )

  2. 上面粗体中的句子是指方法本身的能力。然后,精度和准确性将被理解为飞镖类比所说明的(http://en.wikipedia.org/wiki/Precision_vs._accuracy#Accuracy_versus_precision:_the_target_analogy)。所以,低精度,高精度 => 错误的值被重复命中,精度很高:想象物理时间静止,连续调用 nanoTime() 返回相同的数值,但它与实际耗时不同,因为引用时间由一些常数偏移量。

哪种解释是正确的?我的观点是,解释 2 意味着使用 nanoTime() (通过减去两个返回值)测量时间 差异 将正确到纳秒(因为测量中的恒定误差/偏移量将是消除),而解释 1 不能保证测量之间的那种一致性,因此不一定意味着时差测量的高精度。


2013 年 4 月 15 日更新: System.nanoTime() 的 Java 7 文档已更新以解决可能与之前的措辞混淆的问题。

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.

最佳答案

在 Clojure 命令行中,我得到:

user=> (- (System/nanoTime) (System/nanoTime))
0
user=> (- (System/nanoTime) (System/nanoTime))
0
user=> (- (System/nanoTime) (System/nanoTime))
-641
user=> (- (System/nanoTime) (System/nanoTime))
0
user=> (- (System/nanoTime) (System/nanoTime))
-642
user=> (- (System/nanoTime) (System/nanoTime))
-641
user=> (- (System/nanoTime) (System/nanoTime))
-641

所以本质上,nanoTime 不会每纳秒更新一次,这与人们对其精度 的直觉预期相反。在 Windows 系统中,它在后台使用 QueryPerformanceCounter API(根据 this article ),实际上它似乎提供了大约 640 ns 分辨率(在我的系统中!) .

请注意,nanoTime 本身根本不可能有任何准确度,因为它的绝对值是任意的。只有连续 nanoTime 调用之间的 差异 才有意义。这种差异的(不)准确度大约为 1 微秒。

关于java - System.nanoTime() 的精度与精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com