System.nanoTime只能用于计算时间差,不能用于计算时间的准确度(System.out.println(new Date(System.nanoTime()));这种是绝对错误的)。
Returns the current value of the most precise available system timer, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.
For example, to measure how long some code takes to execute:
1.long startTime = System.nanoTime(); 2.// ... the code being measured ... 3.long estimatedTime = System.nanoTime() - startTime;
2) Java中的System.nano()很慢
System.nano()调用耗时450 nano,超级慢,比new Object()的操作慢100倍。比System.currentMillis()慢20多倍。
经一群无聊好事者查证,System.nanoTime()在linux下的实现,最终调用clock_gettime系统函数。
100万次调用耗时,java语言中System.nanoTime()和C语言中的clock_gettime()调用时间基本一致,所以System.nanoTime()慢的原因就是系统调用clock_gettime。
无聊好事者请注意,自行测试System.nanoTime()性能时,要这样写:
1.for (int i = 0; i < 1000 * 1000; ++i) { 2. long v = System.nanoTime(); 3.}
而不能这样写:
1.for (int i = 0; i < 1000 * 1000; ++i) { 2. System.nanoTime(); 3.}
上面梁总写法区别:
1.public class NanoTimeTest { 2. public static void main(String [] args) { 3. test1(); 4. test2(); 5. } 6. 7. static void test1() { 8. long time = System.currentTimeMillis(); 9. for(int i=0; i < 1000 * 1000; ++i) { 10. System.nanoTime(); 11. } 12. System.out.println("test1:" + (System.currentTimeMillis() - time)); 13. } 14. 15. static void test2() { 16. long time = System.currentTimeMillis(); 17. for(int i=0; i < 1000 * 1000; ++i) { 18. long v = System.nanoTime(); 19. } 20. System.out.println("test2:" + (System.currentTimeMillis() - time)); 21. } 22.}
F:\Java2>java -server NanoTimeTest
test1:500
test2:500
F:\Java2>java -client NanoTimeTest
test1:16
test2:484
3)
Java5+
摩尔定律是一种众所周知的现象,即计算机中的晶体管数量和它的处理速度随时间呈指数规律增长。作为仙童半导体公司(Fairchild Semiconductor)的研发领导人,戈登•摩尔于1965年提出了这一伟大发现。迄今为止,它仍有效。
与Java首次出现的时候相比,当前计算机的速度要快得多,对于很多应用程序而言以毫秒计时已不再能够满足要求。你可能使用过java.lang.System类,利用currentTimeMillis方法来获得一个方法调用或一段代码的定时信息。此方法可以用来度量执行某操作所花费的时间。但是,在运算速度更快的计算机上操作花费的时间可能远小于1毫秒,于是可以在一个for循环中执行此操作上百次或上千次,然后除以循环次数来计算此操作的单位时间。考虑下面的示例:
long startTime = System.currentTimeMillis(); for (int i=0; i<1000; i++) { performOperation(); // something we want to measure } long endTime = System.currentTimeMillis(); long totalTimeInMillis = endTime - startTime; // because the count was 1000, it's easy to get the unit time long unitTimeInMicros = totalTimeInMillis;