Class PerformanceTimer

java.lang.Object
net.sf.basedb.util.timer.PerformanceTimer

public class PerformanceTimer
extends Object
A simple timer for measuring code performance. You can register one or more timers when constructing an object of this class. Typically, there should be one timer for each part of the code (method call) that you want to measure. Use start(int), stop(int) and stopStart(int, int) to start and stop the timers in your code. Example:
PerformanceTimer timer =
  new PerformanceTimer("method1()", "method2()");
timer.start(0);
method1();
timer.stopStart(0, 1);
method2();
timer.stop(1);
System.out.println(timer.toString());
This classes use System.nanoTime() to get the time. All times are returned as nano-second differences between the start and stop time of the timer. Starting and stopping a timer multiple times adds more time to the timer. Use reset(int) to zero a timer.
Version:
2.4
Author:
nicklas
Last modified
$Date: 2012-09-14 09:28:35 +0200 (fr, 14 sep 2012) $
  • Field Summary

    Fields
    Modifier and Type Field Description
    private boolean[] isRunning  
    private String[] names  
    private long[] timers  
  • Constructor Summary

    Constructors
    Constructor Description
    PerformanceTimer​(String... names)
    Create a new timer object for measuring one or more times.
  • Method Summary

    Modifier and Type Method Description
    long getTime​(int index)
    Get the current time of a timer.
    boolean isRunning​(int index)
    Check if a timer is running or not.
    void reset​(int index)
    Reset a timer.
    void resetAll()
    Reset all timers.
    void start​(int index)
    Start the timer with the given index.
    long stop​(int index)
    Stop a running timer.
    void stopAll()
    Stop all running timers
    long stopStart​(int stopIndex, int startIndex)
    Start one timer and stop another.
    String toString()
    Print out time information about each timer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • names

      private final String[] names
    • timers

      private final long[] timers
    • isRunning

      private final boolean[] isRunning
  • Constructor Details

    • PerformanceTimer

      public PerformanceTimer​(String... names)
      Create a new timer object for measuring one or more times.
      Parameters:
      names - The name of the timers
  • Method Details

    • start

      public void start​(int index)
      Start the timer with the given index. This method does nothing if the timer is already running.
      Parameters:
      index - The index of the timer
    • stop

      public long stop​(int index)
      Stop a running timer. This method does nothing if the timer isn't running.
      Parameters:
      index - The index of the timer
      Returns:
      The current time
    • stopStart

      public long stopStart​(int stopIndex, int startIndex)
      Start one timer and stop another.
      Parameters:
      stopIndex - The timer to stop
      startIndex - The timer to start
      Returns:
      The current time of the stopped timer
    • stopAll

      public void stopAll()
      Stop all running timers
    • isRunning

      public boolean isRunning​(int index)
      Check if a timer is running or not.
      Parameters:
      index - The index of the timer
      Returns:
      TRUE if the timer is running, FALSE if it has stopped
    • getTime

      public long getTime​(int index)
      Get the current time of a timer.
      Parameters:
      index - The index of the timer
      Returns:
      The current time of the timer
    • reset

      public void reset​(int index)
      Reset a timer.
      Parameters:
      index - The timer to reset
    • resetAll

      public void resetAll()
      Reset all timers.
    • toString

      public String toString()
      Print out time information about each timer. The times are rounded to millisecond values.
      Overrides:
      toString in class Object