System.currentTimeMillis() 方法真的返回当前时间吗?

发布于 2024-08-14 16:06:52 字数 964 浏览 3 评论 0原文

根据链接中提出的想法,我实现了几种不同的“睡眠方法”。其中一种方法是“二进制睡眠”,如下所示:

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 = 标准差

替代文本 http://img694.imageshack.us/img694/2254/bellcurve.jpg

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 技术交流群。

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

发布评论

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

评论(3

惜醉颜 2024-08-21 16:06:52

不,事实并非如此。它的弟弟 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 than System#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.

巡山小妖精 2024-08-21 16:06:52

根据文档,

 * 返回当前时间(以毫秒为单位)。注意
 * 而返回值的时间单位是毫秒,
 * 值的粒度取决于底层
 * 操作系统可能会更大。例如,许多
 * 操作系统以几十为单位测量时间
 * 毫秒。

Per the docs,

 * Returns the current time in milliseconds.  Note that
 * while the unit of time of the return value is a millisecond,
 * the granularity of the value depends on the underlying
 * operating system and may be larger.  For example, many
 * operating systems measure time in units of tens of
 * milliseconds.
つ可否回来 2024-08-21 16:06:52

您所看到的是分辨率为 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.

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