2.17.2: 2011-06-17

net.sf.basedb.util.timer
Class PerformanceTimer

java.lang.Object
  extended by 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: 2008-09-11 22:08:14 +0200 (Thu, 11 Sep 2008) $

Field Summary
private  boolean[] isRunning
           
private  String[] names
           
private  long[] timers
           
 
Constructor Summary
PerformanceTimer(String... names)
          Create a new timer object for measuring one or more times.
 
Method Summary
 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 Detail

names

private final String[] names

timers

private final long[] timers

isRunning

private final boolean[] isRunning
Constructor Detail

PerformanceTimer

public PerformanceTimer(String... names)
Create a new timer object for measuring one or more times.

Parameters:
names - The name of the timers
Method Detail

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

2.17.2: 2011-06-17