public class PerformanceTimer
extends java.lang.Object
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.Modifier and Type | Field and Description |
---|---|
private boolean[] |
isRunning |
private java.lang.String[] |
names |
private long[] |
timers |
Constructor and Description |
---|
PerformanceTimer(java.lang.String... names)
Create a new timer object for measuring one or more times.
|
Modifier and Type | Method and 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.
|
java.lang.String |
toString()
Print out time information about each timer.
|
private final java.lang.String[] names
private final long[] timers
private final boolean[] isRunning
public PerformanceTimer(java.lang.String... names)
names
- The name of the timerspublic void start(int index)
index
- The index of the timerpublic long stop(int index)
index
- The index of the timerpublic long stopStart(int stopIndex, int startIndex)
stopIndex
- The timer to stopstartIndex
- The timer to startpublic void stopAll()
public boolean isRunning(int index)
index
- The index of the timerpublic long getTime(int index)
index
- The index of the timerpublic void reset(int index)
index
- The timer to resetpublic void resetAll()
public java.lang.String toString()
toString
in class java.lang.Object