Class HiResTimer
Java now provides this natively via System.nanoTime(), so all of this can and should be thrown out some day.
Historical background: Java previously supplied only millisecond resolution, and on Windows this was only around 10-20 millisecond precision; approx 16ms (Win3.1), 55ms (ME/98/95) or 10ms (others)). Other platforms (Linux/Solaris) appear to provide real millisecond resolution.
This class uses JNI to access the Windows QueryPerformanceCounter API. It requires the HiResTimer.dll to be located in the VM's java.library.path. In order to work in WSAD, it will also examine the contents of the 'workspace.root' System property, and attempt to reference it from within the developer's build tree.
If the native library cannot be loaded, this class falls back to Java's standard timer implementation (System.currentTimeMillis).
Note that the Websphere reloading ClassLoader cannot deal with JNI, so this class must exist in an external classpath or JAR which is referenced from the system classpath. The Ant build task 'buildStartupJar' in ext/build.xml can compile the JNI and create this startup JAR.
This file was based on the discussion at http://www.fawcette.com/archives/premier/mgznarch/javapro/2001/08aug01/km0108/km0108-1.asp
Some instructions on building JNI in a cygwin/gcc environment was found at http://www.inonit.com/cygwin/jni/helloWorld/
Documentation on Windows APIs can be found in Windows 2003 Core API download, found at http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
- Author:
- knoxg
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic long
getElapsedMillis
(long timestamp1, long timestamp2) Returns the elapsed time between two timestamps, as a integer number of milliseconds.static long
getElapsedNanos
(long timestamp1, long timestamp2) Returns the elapsed time between two timestamps, as a integer number of microseconds.static long
Return the frequency of the available timer.static long
Returns the current value of the timer, as measured in the frequency supplied bygetFrequency()
.static boolean
Returns true if the Windows HAL (Hardware Abstraction Layer) supports a high-resolution performance counter.static boolean
Returns true if we can use a natively-supplied timer, false otherwisestatic void
Test program.
-
Constructor Details
-
HiResTimer
public HiResTimer()
-
-
Method Details
-
getFrequency
Return the frequency of the available timer.- Returns:
- The granularity of the timer, measured in ticks per second.
-
getTimestamp
Returns the current value of the timer, as measured in the frequency supplied bygetFrequency()
. The starting time for returned values is undefined (i.e. there is no fixed point for timestamp = 0); this value can only be used for relative timing only, or must be correlated with System.currentTimeMillis().- Returns:
- The current value of the timer.
-
isNativeTimerAvailable
Returns true if we can use a natively-supplied timer, false otherwise- Returns:
- true if we can use a natively-supplied timer, false otherwise
-
isHiResTimerAvailable
Returns true if the Windows HAL (Hardware Abstraction Layer) supports a high-resolution performance counter. Note that even if this returns false, we can fall back to a native counter, which is still more precise than the one that Java supplies.- Returns:
- True if high resolution performance is available, false if not.
-
getElapsedMillis
Returns the elapsed time between two timestamps, as a integer number of milliseconds.- Returns:
- the elapsed time between two timestamps, as a integer number of milliseconds.
-
getElapsedNanos
Returns the elapsed time between two timestamps, as a integer number of microseconds.- Returns:
- the elapsed time between two timestamps, as a integer number of microseconds.
-
main
Test program.- Parameters:
args
- Command-line options- Throws:
InterruptedException
- if the Thread.sleep() call was interrupted
-