System.nanoTime / System.currentTimeMillis = 107 (should this be 1e6 ?)

前端 未结 1 1643
清酒与你
清酒与你 2021-01-29 14:16

According to java.lang.System API

currentTimeMillis() Returns the current time in milliseconds

nanoTime() Returns the current value of the run

相关标签:
1条回答
  • 2021-01-29 14:49

    System.nanoTime() has an arbitrary start point; it's not unix epoch. From the Javadoc:

    The value returned represents nanoseconds since some fixed but arbitrary origin time

    So what you're actually calculating there is:

    (unknownOffset + offsetFromEpochInNanos) / offsetFromEpochInMillis
    

    which will almost certainly not be 1e6, unless unknownOffset happens to be arbitrarily zero.

    If you can remove the effect of the unknown offset by subtracting the two times, you can see that the ratio is around 1e6:

    long nanoStart = System.nanoTime();
    long milliStart = System.currentTimeMillis();
    
    Thread.sleep(2000);
    
    long nanoEnd = System.nanoTime();
    long milliEnd = System.currentTimeMillis();;
    
    long nanoDelta = nanoEnd - nanoStart;
    long milliDelta = milliEnd - milliStart;
    
    System.out.println((double) nanoDelta / milliDelta);
    

    Output (running 5 times):

    1000058.3725
    1000045.4705
    999549.1579210395
    1000046.101
    1000038.1045
    

    Ideone demo

    So, pretty close to 1e6.

    Note that it might not be this, because System.currentTimeMillis() doesn't progress smoothly, owing to corrections for clock skew. However, these should be infrequent, so most of the time when you run this code, you'll see roughly 1e6.

    0 讨论(0)
提交回复
热议问题