Package net.sf.basedb.util.timer
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. Usestart(int)
,stop(int)
andstopStart(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 useSystem.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. Usereset(int)
to zero a timer.- Version:
- 2.4
- Author:
- nicklas
- Last modified
- $Date: 2012-09-14 09:28:35 +0200 (fr, 14 sep 2012) $
-
-
Constructor Summary
Constructors Constructor Description PerformanceTimer(String... names)
Create a new timer object for measuring one or more times.
-
Method Summary
All Methods Instance Methods Concrete Methods 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 timerslong
stopStart(int stopIndex, int startIndex)
Start one timer and stop another.String
toString()
Print out time information about each timer.
-
-
-
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 stopstartIndex
- 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.
-
-