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:

1) 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.

2) 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

3) 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...)

4) 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?

Has this been bug reported with the example? And is it a question of nanoTime not functioning properl, or Windows not functioning properly?

I wouldnt call it totally worthless in any case as I\\\'m using it quite successfully in my BHO client at the moment. Ofcourse I\\\'m only using it for split second timing. Timing in lower resolutions I do with System.currentTime Millis.

Got a question about Java and game programming? Just new to the Java Game Development Community? Try my FAQ. Its likely you'll learn something!

Has this been bug reported with the example? And is it a question of nanoTime not functioning properl, or Windows not functioning properly?

Unfortunately, I'm far too lazy and selfish to report it.

Dual core AMDs does something nasty with the timing. It's a well known issue. Some games (painkiller, for example) won't run at all unless you disable one core for them, and nanoTime works horribly bad unless you disable one core.AMD, of course, claims it's not a bug. People who depend on the timing the way they do are the people who get it wrong, but they did release a "fix" for it that's basically just a program that synchronizes the timers every now and then.

The source code (scroll down) for my Infinite Mario Bros game contains a pretty bad work around for the bug after detecting it (ie after time has gone BACKWARDS once, as it does with nanoTime)

I know nothing about dual core CPUs, but I can imagine software knows nothing about them having 2 cores... CPU just feeds data back the same way as normal ones. Again, this is only a assumption, as if there were difference new software should be written just for them.

nanoTime() seems to be a far better option than currentTimeMillis(), at least on Windows. As mentioned above, on Windows the Timer has a 50 ms resolution, far too less for games. But I never ran nanoTime() for a long time or on other PCs with AMD or somewhat. So it might have its drawbacks.

Regarding other timers, I am using 2D only and no GL or such stuff, can you suggest any timer to use? Or is the LWJGL Timer independent from 3D rendering?

That is, if you are running an ancient version of Windows which hardly anyone uses anymore. I myself never experienced timing resolutions that low, even on Win98 (which doesn't mean no Win9x installation has timing resolutions that bad, though).

Quote

Regarding other timers, I am using 2D only and no GL or such stuff, can you suggest any timer to use? Or is the LWJGL Timer independent from 3D rendering?

I don't think the LWJGL timer depends on OpenGL usage, but it seems to be a bit of overkill to use LWJGL only for its timer.Why not use Slick too while you're at it, if you need a good timer and the benefits of OpenGL combined with easy to use 2D rendering?

I have measured currentTimeMillis() being 50 - 55 ms on Windows XP. Maybe it depends on some hardware like mainboard or processor. Maybe its just with Intel processors, or AMD, or such. I cant tell. But I am sure a lot of XP users can copy that currentTimeMillis performs poor on XP.

Do you have any useful test to verify nanoTime() ?When I used nanoTime, it always worked out well, but I never ran more than a few seconds. I dont know if it canmake trouble when running for minutes / hours, if it has temporary errors or so.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org