Class HiResTimer

java.lang.Object
com.randomnoun.common.timer.HiResTimer

public class HiResTimer extends Object
This is some increasingly ancient code to provide microsecond timer resolution in Java using JNI on Windows.

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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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 by getFrequency().
    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 otherwise
    static void
    main(String[] args)
    Test program.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • getFrequency

      public static long getFrequency()
      Return the frequency of the available timer.
      Returns:
      The granularity of the timer, measured in ticks per second.
    • getTimestamp

      public static long getTimestamp()
      Returns the current value of the timer, as measured in the frequency supplied by getFrequency(). 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

      public static boolean 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

      public static boolean 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

      public static long getElapsedMillis(long timestamp1, long timestamp2)
      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

      public static long getElapsedNanos(long timestamp1, long timestamp2)
      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

      public static void main(String[] args) throws InterruptedException
      Test program.
      Parameters:
      args - Command-line options
      Throws:
      InterruptedException - if the Thread.sleep() call was interrupted