In Clojure command line, I get:
user=> (- (System/nanoTime) (System/nanoTime))
0
user=> (- (System/nanoTime) (System/nanoTime))
0
user=> (- (System/nanoTime) (System/nanoTime))
-641
user=> (- (System/nanoTime) (System/nanoTime))
0
user=> (- (System/nanoTime) (System/nanoTime))
-642
user=> (- (System/nanoTime) (System/nanoTime))
-641
user=> (- (System/nanoTime) (System/nanoTime))
-641
So essentially, nanoTime
doesn’t get updated every nanosecond, contrary to what one might intuitively expect from its precision. In Windows systems, it’s using the QueryPerformanceCounter
API under the hood (according to this article), which in practice seems to give about 640 ns resolution (in my system!).
Note that nanoTime
can’t, by itself, have any accuracy at all, since its absolute value is arbitrary. Only the difference between successive nanoTime
calls is meaningful. The (in)accuracy of that difference is in the ballpark of 1 microsecond.