gpt4 book ai didi

java - System.currenTimeInMillis() 与 System.nanoTime()

转载 作者:IT老高 更新时间:2023-10-28 20:33:29 24 4
gpt4 key购买 nike

我知道 System.nanoTime() 现在是在 System.currentTimeInMillis() 上测量时间的首选方法。第一个明显的原因是 nanoTime() 提供了更精确的计时,另一个原因是我读到后者受系统实时时钟调整的影响。 “受系统实时时钟影响”是什么意思?

最佳答案

在这种情况下,我发现以下博客文章摘录很有用:

If you are interested in measuring absolute time then always use System.currentTimeMillis(). Be aware that its resolution may be quite coarse (though this is rarely an issue for absolute times.)

If you are interested in measuring/calculating elapsed time, then always use System.nanoTime(). On most systems it will give a resolution on the order of microseconds. Be aware though, this call can also take microseconds to execute on some platforms.

Clocks and Timers - General Overview大卫·福尔摩斯

由于 System.currentTimeMillis() 依赖于系统 time of day 时钟,因此对时间的调整是合法的,以保持准时。

这里的调整是什么意思?以 Linux 中 CLOCK_REALTIME 的描述为例:

System-wide clock that measures real (i.e., wall-clock) time. Setting this clock requires appropriate privileges. This clock is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.

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

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