今天我做了一些快速的基准测试的System.nanoTime()
和System.currentTimeMillis()
高速性能:为什么System.nanoTime()方法比System.currentTimeMillis()更慢?
long startTime = System.nanoTime();
for(int i = 0; i < 1000000; i++) {
long test = System.nanoTime();
}
long endTime = System.nanoTime();
System.out.println("Total time: "+(endTime-startTime));
这是结果:
System.currentTimeMillis(): average of 12.7836022/function call
System.nanoTime(): average of 34.6395674/function call
为什么在运行速度这么大的差异?
基准系统:
Java 1.7.0_25
Windows 8 64-bit
CPU: AMD FX-6100
可能的重复[为什么System.nanoTime()和System.currentTimeMillis()漂移如此之快?](http://stackoverflow.com/questions/5839152/why-do-system-nanotime-and-system - 当前timemillis漂移分开如此迅速) – chrylis
这篇文章可能会回答你的问题:http://stackoverflow.com/a/5839267/658907 – matts
'nanoTime'比'currentTimeMillis'更精确,这可能是原因。 – Thomas