System.currentTimeMillis() 方法真的返回当前时间吗?
根据链接中提出的想法,我实现了几种不同的“睡眠方法”。其中一种方法是“二进制睡眠”,如下所示:
while (System.currentTimeMillis() < nextTimeStamp)
{
sleepTime -= (sleepTime / 2);
sleep(sleepTime);
}
因为在开始时会检查是否已达到下一个时间步,所以我预计该方法运行时间太长。但模拟误差的累积分布(预期时间 - 实时)如下所示: alt文本 http://img267.imageshack.us/img267/4224/errorvscummulativearran.jpg
有人知道为什么我会得到这个结果吗?也许方法 System.currentTimeMillis() 并没有真正返回当前时间?
BR,
Markus
@irreputable
当我进行评估时,我还使用德国统计程序创建了一条钟形曲线。由于无法更改标题,因此以下是所有相关项目的英文翻译:
Häufigkeit = 频率
Fehler = 误差
Mittelwert = 平均值
Std-Abw = 标准差
Based on ideas presented in link I implemented several different "sleep methods". One of this methods was the "binary sleep", which looks like this:
while (System.currentTimeMillis() < nextTimeStamp)
{
sleepTime -= (sleepTime / 2);
sleep(sleepTime);
}
Because the check if the next time step is already reached takes place at the beginning I, would expect that the method is running too long. But the cummulative distribution of the simulation error (expected time - real time) looks like this: alt text http://img267.imageshack.us/img267/4224/errorvscummulativearran.jpg
Does somebody has an idea why I'm getting this results? Maybe the method System.currentTimeMillis() does not really return the current time?
BR,
Markus
@irreputable
When I made the evaluation I also created a bell curve by using a german statistic program. Because it was not possible to change caption, here is the english translation of all relevant items:
Häufigkeit = frequency
Fehler = error
Mittelwert = average
Std-Abw = standard deviation
alt text http://img694.imageshack.us/img694/2254/bellcurve.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,事实并非如此。它的弟弟
System#nanoTime()
的精度比System#currentTimeMillis()
。除了 Javadocs 中的答案(单击上面的链接)之外,这个主题也在这里讨论了几次。搜索“currenttimemillis vs nanotime”,您将找到以下每个主题:System.currentTimeMillis 与 System.nanoTime。
No it does not. Its young brother
System#nanoTime()
has a much better precision thanSystem#currentTimeMillis()
.Apart from the answers in their Javadocs (click at the links here above), this subject was discussed several times here as well. Do a search on "currenttimemillis vs nanotime" and you'll get under each this topic: System.currentTimeMillis vs System.nanoTime.
根据文档,
Per the docs,
您所看到的是分辨率为 15 毫秒的底层时钟。这是操作系统和操作系统的一个功能。中断率。 Linux 内核有一个补丁可以将此分辨率提高到 1ms,我不确定 Windows 是否如此。已经有很多关于此的帖子。
What you are seeing is the underlying clock resolving to 15ms resolution. This is a feature of the OS & interrupt rate. There is a patch for the linux kernel to increase this resolution to 1ms, I'm not sure about windows. There have been a number of posts about this already.