Why I get a negative elapsed time using System.nanoTime()?

[亡魂溺海] 提交于 2019-11-29 03:30:19

The nanoTime can be taken of the CPU clock cycle counter. As different CPUs can be started at slightly different times, the clock counter can be different on different CPUs. Linux corrects for this, but older versions of Windows do not. (I assume you have two CPUs which were started 3 ms apart)

You should also see positive jumps of over 2.5 ms occasionally.

Try

if (cost < 0 || cost > 2000000) {

And you should see some jump forward and some jump backward as the process is switched between CPUs.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!