I’ve read quite a bit about System.nanoTime() in Java 1.5+, both on this forum and on the web, and here’s a bit of what I’ve learned:
-
nanoTime attempts to use the most precise timer available, but can only be used to measure time intervals (not walltime) – fortunately, this is all you need in a game.
-
I’ve read that nanoTime uses QueryPerformanceCounter and some have suggested that this can cause strage results on multi-core machines as well as on machines (like many laptops) that vary the clockspeed of the cpu
-
somewhat in conflict with (2), I’ve also read that QueryPerformanceCounter is preferred to RDTSC (on Intel chips) since RDTSC does NOT take into account changes in clock speed (thus implying that QPC does…)
-
In a simple loops that executes System.nanoTime() and stores the result in an array, I see values as low as 1.2 micro seconds, but values as high as 5000 micro seconds if I run the loop for 10000 iterations or so. Some people have noted that varying the clock speed of the CPU can cause discontinuities in the results of nanoTime(), so perhaps that explains the results, but it could also presumably be due to the OS running other processes.
The bottom line is: is there any consensus as to whether System.nanoTime() is good or evil? If evil, is there a suggested alternative timer?

