PUBLIC OBJECT

The Tick

To a human the computer's clock is fluid, constantly running through imperceptibly-brief milliseconds. But to the CPU, clock changes are rare events. You can run lots and lots of code between clock ticks.

High-precision timers tick more frequently, but they don't solve the problem. How many distinct timestamps should I observe when running this code?

    long[] timestamps = new long[1000];
    for (int i = 0; i < timestamps.length; i++) {
      timestamps[i] = System.nanoTime();
    }
    for (long timestamp : timestamps) {
      System.out.println(timestamp);
    }

If you guessed 1000, you guessed wrong. I saw 75 distinct timestamps on my MacBook Pro. The output stair-steps, staying on one value for several observations and then jumping to another. Here's a sample of the output:

  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026068000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026069000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000
  1381031468026070000

On this particular Mac and JVM, the source of System.nanoTime() has microsecond precision. That's typical, and the method's Javadoc doesn't hide it:

"This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change."

The consequence of this is that sometimes events will share a timestamp. Our code can't assume that timestamps can provide a total ordering of events. But for that I like serial numbers like AtomicInteger's getAndIncrement().