[concurrency-interest] java.lang.System.nanoTime and QueryPerformanceCounter-"Bug"

Michael Mayr Michael.Mayr@web.de
Wed, 24 Dec 2003 03:24:11 +0100


Hi David,

David Holmes wrote:
"There is no notion of an "invalid time" as negative values from
nanoTime are possible. The only use for the return value from nanoTime
is to compare it with another return value from nanoTime.

The implementation is responsible for using the most sensible timing
mechanism to provide nanoTime and to account for as many
idiosyncracies as possible."

Ok, I understand. Then what do you think of the following strategies:

With every call to System.nanoTime() you compare its results with the results of millisecond-accurate Time (which has always been available on Windows - but still System.currentTimeMillis() is only accurate to 10 ms (Win2k/NT) or 50 ms (Win9x). 
See http://developer.java.sun.com/developer/bugParade/bugs/4423429.html   
But that is another story). 

This way you can determine if a leap has occured and in this case return the millisecond accurate time (which is IMHO better than to return the nanosecond accurate time plus leap seconds). But you should give the developer a hint that this result may have only one millisecond accuracy and not just silently provide him with a result with accuracy LessThanNano when he expects nano accuracy because of the name of the method. This way the developer can resort to other strategies if the accuracy is not sufficient. E. g.  he could then profile 1000 calls to a method and then divide the result by 1000.

I mean something like this:

public static TimeResult nanoTime();

with:

public class TimeResult {
  long counts;
  java.util.concurrent.TimeUnit units;
}

which is IMHO nicer than the other accurate solution:

public static long nanoTime() throws InvalidTimeException, NotSupportedOnThisPlatformException

I'd like to hear what you think. 
Merry christmas.

Michael


______________________________________________________________________________
Nachrichten, Musik und Spiele schnell und einfach per Quickstart im 
WEB.DE Screensaver - Gratis downloaden: http://screensaver.web.de/?mc=021110