今天我做了一些快速的基準測試的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