比较不同机器产生的 System.nanoTime() 值

发布于 2024-12-13 16:58:18 字数 363 浏览 0 评论 0原文

比较两台不同机器上调用 System.nanoTime() 所产生的两个值是否正确?我会说不,因为 System.nanoTime() 通过使用 返回相对于某个任意点时间的纳秒精确时间时间戳计数器 (TSC) 取决于处理器。

如果我是对的,有没有一种方法(在Java中)可以捕获两台不同机器上的瞬间,并以至少微秒精度甚至纳米级精度(安全地)比较这些值?

System.currentTimeMillis() 不是一个解决方案,因为它没有返回线性增加的时间戳数量。用户或NTP等服务可以随时更改系统时钟,时间会前后跳跃。

Is it correct to compare two values resulting from a call to System.nanoTime() on two different machines? I would say no because System.nanoTime() returns a nanosecond-precise time relative to some arbitrary point time by using the Time Stamp Counter (TSC) which is processor dependent.

If I am right, is there a way (in Java) to capture an instant on two different machines and to compare (safely) these values with at least a microsecond precision or even nanotime precision?

System.currentTimeMillis() is not a solution because it is not returning a linearly increasing number of time stamps. The user or services such as NTP can change the system clock at any time and the time will leap back and forward.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

邮友 2024-12-20 16:58:18

您可能想研究各种可用的时钟同步算法。显然,精确时间协议可以让您在 LAN 上获得亚微秒级的精度。

如果您不需要特定的时间值,而是想知道各种事件的顺序,您可以使用 兰波特时间戳

You might want to look into the various clock synchronization algorithms available. Apparently the Precision Time Protocol can get you within sub-microsecond accuracy on a LAN.

If you don't need a specific time value but rather would like to know the ordering of various events, you could for instance use Lamport timestamps.

So尛奶瓶 2024-12-20 16:58:18

您不能在两台不同的机器之间使用nanoTime。对于 Java API 文档:

此方法只能用于测量经过的时间,不能用于测量经过的时间。
与系统或挂钟时间的任何其他概念相关。价值
返回表示自某个固定但任意时间以来的纳秒
(也许在未来,所以值可能是负数)。

无法保证 nanoTime 相对于任何时基。

You cannot use nanoTime between two different machines. For the Java API docs:

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

There's no guarantee that nanoTime is relative to any timebase.

独守阴晴ぅ圆缺 2024-12-20 16:58:18

这是一个处理器和取决于操作系统 问:例如,查看 POSIX 时钟,有高精度时间感知时间戳(例如 CLOCK_REALTIME 返回纳秒时间值)和高精度任意时间时间戳(例如 CLOCK_MONOTONIC)(注意:这两者之间的区别是这个很好地解释了答案)。

后者通常是自从盒子启动以来的时间,因此没有办法在服务器之间准确地比较它们,除非你首先有高精度时钟同步(例如另一个答案中引用的 PTP)(因为那时你会能够在它们之间共享偏移量)。

NTP 是否足够适合您取决于您​​要测量的内容。例如,如果您尝试测量几百微秒的间隔(例如连接到同一交换机的盒子),那么您的结果将很粗糙,在另一个极端,如果您的服务器完全位于不同的地理位置,则 NTP 可能会非常好(例如伦敦到纽约)这意味着时钟同步效果(只要不是太远)会被位置之间的延迟所淹没。

FWIW 从 java 访问此类时钟所需的 JNI 非常简单。

This is a processor & OS dependent Q. Looking at POSIX clocks, for example, there are high precision time of day aware timestamps (e.g. CLOCK_REALTIME returns a nano epoch time value) and high precision arbitrary time timestamps (e.g. CLOCK_MONOTONIC) (NB: the difference between these 2 is nicely explained in this answer).

The latter is often something like time since the box was booted and therefore there's no way to accurately compare them across servers unless you have high precision clock sync (e.g. PTP as referenced in the other answer) in the first place (as then you'd be able to share an offset between them).

Whether NTP is good enough for you depends on what you're trying to measure. For example if you're trying to measure an interval of a few hundred micros (e.g. boxes connected to the same switch) then your results will be rough, at the other extreme NTP can be perfectly good if your servers are in different geographical locations entirely (e.g. London to NY) which means the clock sync effect (as long as it's not way way off) is swamped by the latency between the locations.

FWIW the JNI required to access such clocks from java is pretty trivial.

一片旧的回忆 2024-12-20 16:58:18

您可以将时间同步到当前时间毫秒。然而,即使您使用 NTP,机器之间的偏差也可能为 1 毫秒到 10 毫秒。机器之间实现微秒级同步的唯一方法是使用专业硬件。

nanoTime 保证以相同的方式确定或在两个不同的操作系统上具有相同的分辨率。

You can synchronize the time to current time millis. However even if you use NTP this can drift by 1 ms to 10 ms between machines. The only way to be micro-second synchronization between machines is to use specialist hardware.

nanoTime is guaranteed to be determined the same way or have the same resolution on two different OSes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文