下面的程序输出了毫秒计时器与纳秒计时器的最大偏离。
public static void runWithSpin(){
long nowNanos=0,startNanos=0;
long startMills=System.currentTimeMillis();
long nowMills=startMills;
while (nowMills==startMills){
startNanos=System.nanoTime();
nowMills=System.currentTimeMillis();
}
startMills=nowMills;//上面做的事情是为了保证startMills的时间真的是开始时间,而不是约等于之后的开始时间
double maxDrift=0;
long lastMillis;
while (true){
lastMillis=nowMills;
while (nowMills-lastMillis<1000){
nowNanos=System.nanoTime();
nowMills=System.currentTimeMillis();
}
//至此,刚好经过1ms
long durationMillis=nowMills-startMills;//毫秒级差别
double driftNanos=1000000*((double)(nowNanos-startNanos)/1000000-durationMillis);
if (Math.abs(driftNanos)>maxDrift){
System.out.println("now-start"+durationMillis+",纳秒级与毫秒级的差别:"+driftNanos);
}
}
}
now-start1000,纳秒级与毫秒级的差别:-150564.00000003123
now-start2000,纳秒级与毫秒级的差别:-822562.9999999455
now-start3000,纳秒级与毫秒级的差别:-498461.00000013394
now-start4000,纳秒级与毫秒级的差别:-172308.00000015734
now-start5000,纳秒级与毫秒级的差别:-848820.0000001597
now-start6000,纳秒级与毫秒级的差别:-491896.9999998808
now-start7000,纳秒级与毫秒级的差别:-199795.00000044936
now-start8000,纳秒级与毫秒级的差别:-873845.0000000739
now-start9000,纳秒级与毫秒级的差别:-545640.0000002759
now-start10000,纳秒级与毫秒级的差别:-224819.999999454
now-start11000,纳秒级与毫秒级的差别:99281.99999922072
从结果可以看到11秒中,每1000毫秒,最大有0.8毫秒(有时会更大)的差别。
对于间隔时间长的时间,nanoTime基本是不可信的,只能用它测量较短的时间间隔。
要想做好性能调优,需要直到Java平台上得到的时间测量结果的局限性。
准确度是对测量中系统性错误的度量。可能存在准确但不太精确的测量结果。
粒度是最快计时器的频率–很可能是10纳秒范围内的中断计时器。可以肯定几乎一起发生,但时间不同。大多数情况下,应用程序是得不到这些特别短的时间间隔的。
分布式网络计时
通常来说,很难得到用于跨越几台机器的工作流的准确时序。