深入研究 System.currentTimeMillis(),我们可以期望它有多精确?毫秒实际精度是什么意思? [英] Digging deeper into System.currentTimeMillis(), how precise can we expect it to be? What does millisecond actually precision mean?

查看:28
本文介绍了深入研究 System.currentTimeMillis(),我们可以期望它有多精确?毫秒实际精度是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请仅在您完全理解问题后才回答.

不要关闭,因为不存在类似的问题.

我知道 System.nanoTime() 从任意随机"中给出 nsJVM 启动后的点.而且我知道 System.currentTimeMillis() 只给出 ms 精度.

我正在寻找的是证明,并保持开放的心态,一旦我们尝试定义什么ms更改并不准确的假设>确切的意思.

Exact 在我的世界里意味着每次我们要注册一个新的 ms 比如说,我们从 97ms98ms99ms 等等,每次我们得到更新,通过任何机制,我们不能指望至少 观察到 Java 给我们 纳秒 精度在开关.

我知道,我知道.这听起来很奇怪,但问题来了,ms 开关的准确度如何?

似乎是当您反复询问 System.nanoTime() 时,您将能够获得具有纳秒分辨率的线性图.

如果我们同时在 System.nanoTime() 之后询问 System.currentTimeMillis() 并且我们忽略命令成本的差异,看起来好像不会有相同分辨率的线性图.毫秒图将 +-250ns.

这是意料之中的,但我找不到任何关于误差幅度的信息,或毫秒的准确性.

这个问题也适用于秒精度,或小时精度、日、年等.到了年,误差有多大?

ms 到来时,ns 上的误差有多大?

System.currenTimeMillis() 不能相信与 System.nanoTime() 保持线性,我们不能指望 System.currenTimeMillis()code> 以跟上 ns 精度.

但是误差有多大?在计算?在 Java 中,在 unix 系统中?

解决方案

来自文档:

<块引用>

"注意,虽然返回值的时间单位是毫秒,但值的粒度取决于底层操作系统,可能会更大.例如,许多操作系统以几十毫秒为单位测量时间.

<块引用>

有关计算机时间"和计算机时间"之间可能出现的细微差异的讨论,请参阅类日期的说明.和协调世界时 (UTC)."

所以调用的精度和准确度都是不确定的.他们把责任推给操作系统并耸了耸肩.我怀疑 250 ns 是对其质量的准确度量.差距可能比这大得多.几十毫秒"根据文档是一个更有可能的值,尤其是跨多个系统.

此外,他们基本上也否认对 UTC 的任何了解.轻微差异"被允许,无论这意味着什么.从技术上讲,这完全允许任何值,因为究竟什么是轻微"?根据您的观点,可能是一秒或一分钟.

最后,系统时钟可能被操作系统的人错误配置,然后一切都消失了.

Please only answer WHEN you fully comprehend the question.

Do not close down, as there does not exist a similar question.

I am aware of System.nanoTime() gives ns from an arbitrary "random" point after the JVM starts. And I am aware that System.currentTimeMillis() only gives ms precision.

What I am looking for is for the PROOF and keep an open mind, to the hypothesis that the ms changes are not exact once we try to define what exact means.

Exact would in my world mean that everytime we were to register a new ms say, we go from 97ms, 98ms, 99ms and so forth, on every time we get an update, through whatever mechanisms, we can not expect at least observed Java to give us nanosecond precision at the switches.

I know, i know. It sounds weird to expect that, but then the question comes, how accurate are the ms switches then?

It appears to be that when you ask System.nanoTime() repeatedly you would be able to get a linear graph with nanosecond resolution.

If we at the same time ask System.currentTimeMillis() right after System.nanoTime() and we disregard the variance in cost of commands, it appears as if there would be not a linear graph on the same resolution. The ms graph would +-250ns.

This is the to be expected, yet I can not find any information on the error margin, or the accuracy of the ms.

This issue is there for second precision as well, or hour precision, day, year, and so forth. When the year comes, how big is the error?

When the ms comes, how big is the error in terms on ns?

System.currenTimeMillis() can not be trusted to stay linear against System.nanoTime() and we can not expect System.currenTimeMillis() to keep up with ns precision.

But how big is the error? In computing? In Java, in unix systems?

解决方案

From the documentation:

"Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC)."

So both the precision and accuracy of the call is undefined. They pass the buck to the OS and shrug. I doubt that 250 ns is an accurate measure of its quality. The gap is likely much larger than that. "Tens of milliseconds" as per the documentation is a much more likely value, especially across multiple systems.

Also they essentially disavow any knowledge of UTC as well. "Slight discrepancies" are allowed, whatever that means. Technically this allows any value at all, because what exactly is "slight?" It could be a second or a minute depending on your point of view.

Finally the system clock could be misconfigured by the person operating the system, and at that point everything goes out the window.

这篇关于深入研究 System.currentTimeMillis(),我们可以期望它有多精确?毫秒实际精度是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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