System.nanoTime(): good or evil?

public class MRes{
	public static void main(String[]args){
		long smallestDelta=Long.MAX_VALUE;
		long start=System.currentTimeMillis();
		long current=start;
		long last=start;
		long delta;
		System.out.println("testing...");
		while(current-start<5000){
			current=System.currentTimeMillis();
			delta=current-last;
			if(delta!=0&&delta<smallestDelta)
				smallestDelta=delta;
			last=current;
		}
		System.out.println(System.getProperty("os.name")+" ["+
			System.getProperty("os.version")+"]["+System.getProperty("os.arch")+"]"
		);
		System.out.println("smallest delta: "+smallestDelta+"msec");
	}
}

gives me:

testing...
Windows 2000 [5.0][x86]
smallest delta: 10msec

testing…
Windows XP [5.1][x86]
smallest delta: 15msec

testing…
Mac OS X [10.4.9][i386]
smallest delta: 1msec

The Mac result was as expected. The XP one really baffles me tho.

Same here:

testing…
Windows XP [5.1][x86]
smallest delta: 15msec

and here as well:

testing…
Windows XP [5.1][x86]
smallest delta: 15msec

testing...
Linux [2.6.20-15-generic][i386]
smallest delta: 1msec

Hippies are good at some things I suppose…

Does anybody know why currentTimeMillis on Windows is so rough?

Maybe we should submit an enhancement request to have 1ms precision on Windows too (it’s not a bug since currentTimeMillis doesn’t guarantee anything).

Does anybody know why currentTimeMillis on Windows is so rough?

It uses the OS’ tick counter, whose resolution depends on the OS.

Here is some overview:
http://www.gamedev.net/reference/programming/features/timing/

As you can see TGT has (had?) a few issues, too. However, QPC has a lot more.

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 can
make trouble when running for minutes / hours, if it has temporary errors or so.

-JAW

did you read the whole thread? you realize that it’s only a problem on multi-core cpus? Sorry if you are talking about them, I got impression that you aren’t.

you realize that it’s only a problem on multi-core cpus?

Heh. That would be nice. But there are a dozen different issues with QPC.

I got a single core and QPC is unusable (QPC leaping).

When using System.nanoTime() on my computer with a single-core CPU, the game I wrote would suddenly pause for about half a second every once in a while (about once per level). When using the millisecond timer instead, this problem stopped.

So System.nanoTime() has problems on single-core processors (or at least on mine) as well. System.nanoTime() is evil.

I’ve talked with some other (non-java) developers about the issue, and all of them (okay, they were just 3 people :)) use 1ms timing instead of QPC for exactly the same reasons.

damn Sun developers, why did they used damn QPC :slight_smile: … is there a RFE or bug report on this? It has to be.
I’ve heard LWJGL times is good, why don’t they just use that, or whatever it’s based on.

…I suppose because the RFE it was based on required high precision, and LWJGL’s timer is just 1ms.
I’d rather see a currentTimeMillis implementation with 1ms precision on Windows.

Somehow, I would like to see that. However, since TGT is something completely different (and may not work properly on some systems, too) a new method would be required if you don’t want to create regressions.

The most logical name would be…
System.milliTime() (although… I would like msecTime() more ;))