在Java中需要多长时间?在Java中测量延迟时间 [英] How long it takes for one second in Java? Measure latency time in Java
问题描述
我不想改变这个代码,我只对JVM,OS或内核定制/配置感兴趣。
我有一个循环(1000 x 1ms)
public static void main(String [] args)throws InterruptedException {
long start = System.nanoTime();
for(int i = 0; i <1000; i ++){
Thread.sleep(TimeUnit.MILLISECONDS.toMillis(1));
}
long duration = System.nanoTime() - start;
System.out.println(Loop duration+
duration / TimeUnit.MILLISECONDS.toNanos(1)+ms。);
在Fedora 20内核3.12上,这个循环需要1055 ms。
这是相当不错的结果,平均值超过 1100ms 。
$ b 是使用自定义JVM标记或操作系统配置可以使此代码更快?
循环持续时间1055 ms。
不幸的是,我的问题被误解了。 / b>
实时Java被放弃了,所以使用Realtime java的建议是无效的。
研究这个测试在一些Windows机器上有最好的结果。
在测试过的Windows 8.1上,这个测试打印精确到1000ms。
其他结果:
- Mac OS X 10.9.1与Java 1.7。
$ b
$ b参考资料
/ h2>
I don't want to change this code, I'm only interested in JVM, OS or kernel customization/configuration for best results!
I have one second loop (1000 x 1ms)
public static void main(String[] args) throws InterruptedException {
long start = System.nanoTime();
for (int i = 0; i < 1000; i++ ) {
Thread.sleep(TimeUnit.MILLISECONDS.toMillis(1));
}
long duration = System.nanoTime() - start;
System.out.println("Loop duration " +
duration / TimeUnit.MILLISECONDS.toNanos(1) + " ms.");
}
On my Fedora 20 with kernel 3.12 this loop needs 1055 ms.
This is pretty good result, average is more than 1100ms.
Is possible to make this code faster with custom JVM flags or OS configuration?
Loop duration 1055 ms.
Unfortunately, my question was misunderstood.
Realtime java is abandoned, so advice for using Realtime java is not valid.
After some researches this test has best results on some Windows machines.
On tested Windows 8.1 this tests prints exactly 1000ms.
Other results:
- Mac Os X 10.9.1 with Java 1.7.0_25 : 1180 - 1190ms
- Ubuntu 12.04/Corei3/4GB : 1122 ms
References
- Granularity of Timer seems variable in Windows 7
- Inside the Hotspot VM: Clocks, Timers and Scheduling Events - Part I - Windows
- Part II: Adjustment of System Time
这篇关于在Java中需要多长时间?在Java中测量延迟时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!