System.currenTimeInMillis()vs System.nanoTime() [英] System.currenTimeInMillis() vs System.nanoTime()

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

问题描述

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

I know that System.nanoTime() is now the preferred method for measuring time over System.currentTimeInMillis() . The first obvious reason is nanoTime() gives more precise timing and the other reason I read that the latter is affected by adjustments to the system’s real-time clock. What does "getting affected by systems real-time clock " mean ?

推荐答案

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

In this case I've found following blog post excerpt useful:


如果您对测量绝对时间感兴趣,请始终使用
System.currentTimeMillis的()。请注意,它的分辨率可能非常粗糙(尽管这绝对是绝对时间的问题。)

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

如果您对测量/感兴趣计算经过时间,然后
始终使用 System.nanoTime()。在大多数系统上,它将以微秒为单位给出
的分辨率。但请注意,此调用
也可能需要几微秒才能在某些平台上执行。

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.

时钟和计时器 - 概述作者David Holmes

Clocks and Timers - General Overview by David Holmes

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

Since System.currentTimeMillis() is relying on the systems time of day clock, adjustments to the time are legitimate, in order to keep it on time.

这里的调整是什么意思?例如,从Linux看一下 CLOCK_REALTIME 的描述:

What means adjustments here? Take for instance a look at the description of CLOCK_REALTIME from Linux:


System-测量真实(即挂钟)时间的宽时钟。
设置此时钟需要适当的权限。这个时钟是
受系统时间不连续跳跃影响(例如,如果
系统管理员手动更改时钟
),以及
由adjtime(3)和NTP执行的增量调整。

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.

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

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