System.currentTimeMillis 与 System.nanoTime [英] System.currentTimeMillis vs System.nanoTime

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

问题描述

准确度对比.精度

我想知道的是,在我的游戏中更新对象的位置时,我应该使用 System.currentTimeMillis() 还是 System.nanoTime()?他们的运动变化与自上次通话以来经过的时间成正比,我希望尽可能精确.

我读到不同操作系统之间存在一些严重的时间分辨率问题(即 Mac/Linux 的分辨率接近 1 毫秒,而 Windows 的分辨率为 50 毫秒??).我主要是在 Windows 上运行我的应用,50 毫秒的分辨率似乎很不准确.

还有比我列出的两个更好的选择吗?

有什么建议/意见吗?

解决方案

如果您只是想对经过时间进行极其精确的测量,请使用 System.nanoTime().System.currentTimeMillis() 将为您提供自纪元以来最准确的可能经过的时间(以毫秒为单位),但 System.nanoTime() 为您提供纳秒级的精确时间,相对于一些任意点.

来自 Java 文档:

<块引用>

public static long nanoTime()

返回最精确的可用系统计时器的当前值,以纳秒为单位.

此方法只能用于测量经过的时间,而不是与任何其他系统概念相关或挂钟时间.返回的值代表纳秒,因为一些固定但任意的起源时间(可能在未来,所以价值观可能是消极的).该方法提供纳秒精度,但不是必须是纳秒精度.不保证是关于如何值经常变化.差异在跨越更大的连续呼叫中比大约 292 年 (263纳秒)不会准确由于数值计算经过的时间溢出.

例如,衡量一些代码执行所需的时间:

long startTime = System.nanoTime();//... 被测量的代码 ...长估计时间 = System.nanoTime() - startTime;

另见:JavaDoc 系统.nanoTime()JavaDocSystem.currentTimeMillis() 了解更多信息.

Accuracy Vs. Precision

What I would like to know is whether I should use System.currentTimeMillis() or System.nanoTime() when updating my object's positions in my game? Their change in movement is directly proportional to the elapsed time since the last call and I want to be as precise as possible.

I've read that there are some serious time-resolution issues between different operating systems (namely that Mac / Linux have an almost 1 ms resolution while Windows has a 50ms resolution??). I'm primarly running my apps on windows and 50ms resolution seems pretty inaccurate.

Are there better options than the two I listed?

Any suggestions / comments?

解决方案

If you're just looking for extremely precise measurements of elapsed time, use System.nanoTime(). System.currentTimeMillis() will give you the most accurate possible elapsed time in milliseconds since the epoch, but System.nanoTime() gives you a nanosecond-precise time, relative to some arbitrary point.

From the Java Documentation:

public static long nanoTime()

Returns the current value of the most precise available system timer, 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). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

For example, to measure how long some code takes to execute:

long startTime = System.nanoTime();    
// ... the code being measured ...    
long estimatedTime = System.nanoTime() - startTime;

See also: JavaDoc System.nanoTime() and JavaDoc System.currentTimeMillis() for more info.

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

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