|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectes.iac.ing.codeso.gen.Stopwatch
@NotThreadSafe public class Stopwatch
Modelled on the stopwatch facility of the typical digital watch this class provides a simple facility for general purpose timing.
The class is intended primarily for instrumenting code to aid in optimisation.
The current implementation makes use of the Java 1.5 System.nanotime
method and inherits the same quality of service guarantees.
To avoid synchronisation overhead this class is NOT thread-safe. If synchronisation is required this should be achieved externally.
Nested Class Summary | |
---|---|
static class |
Stopwatch.TimeUnit
An enumeration which modifies the time units when invoking methods on this class. |
Field Summary | |
---|---|
private boolean |
running
Holds the current state of the stopwatch: running or stopped. |
private long |
startTime
A timestamp which keeps track of the instant in which the stopwatch was last started. |
private long |
stopTime
A timestamp which keeps track of the instant in which the stopwatch was last stopped. |
private long |
totalElapsedTimeWhenLastStopped
Holds the the sum of all the completed time intervals. |
Constructor Summary | |
---|---|
Stopwatch()
Constructs a new stopwatch |
Method Summary | |
---|---|
long |
getElapsedTime(Stopwatch.TimeUnit units)
Gets the total elapsed time in a time unit specified by the user. |
java.lang.String |
getElapsedTimeAsFormattedString()
Gets the total elapsed time as a string. |
long |
getElapsedTimeInMillisecondsAsLong()
Gets the total elapsed time in the default time unit, which is milliseconds. |
long |
getIntervalTime(Stopwatch.TimeUnit units)
Gets the interval time in a time unit selectable by the user. |
private long |
getNanosecondTimeStamp()
Gets the current timestamp in nanoseconds with the best resolution possible on the system. |
void |
reset()
Resets the stopwatch setting the interval time and total elapsed time back to zero. |
void |
start()
Starts (or restarts) the stopwatch, initiating a new timing interval. |
void |
stop()
Stops the stopwatch, ending the current timing interval. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private boolean running
private long startTime
private long stopTime
private long totalElapsedTimeWhenLastStopped
Constructor Detail |
---|
public Stopwatch()
Method Detail |
---|
public void start()
This method should only be invoked when the stopwatch is stopped.
This method does NOT reset the total elapsed time.
java.lang.IllegalStateException
- if the timer is already runningpublic void stop()
This method should only be invoked when the stopwatch is running.
java.lang.IllegalStateException
- if the timer is already stoppedpublic void reset()
This method can be called at any time, regardless of whether the stopwatch is running or stopped.
public java.lang.String getElapsedTimeAsFormattedString()
The total elapsed time is is the summation of all the timing intervals that have occurred since the stopwatch was last reset.
The returned string has the format ssss.uuuuuu where s denotes the elapsed time in seconds, and u denotes the fractional part in microseconds.
public long getElapsedTimeInMillisecondsAsLong()
The total elapsed time is is the summation of all the timing intervals since the stopwatch was last reset.
public long getElapsedTime(Stopwatch.TimeUnit units)
The total elapsed time is is the summation of all the timing intervals since the stopwatch was last reset.
units
- specifying the time unit in which the results are to be
returned.
public long getIntervalTime(Stopwatch.TimeUnit units)
When the stopwatch is running this method returns the elapsed time
since the last invocation of the start
method. When
stopped this method returns the duration of the previous time
interval.
units
- specifying the time unit in which the results are to be
returned.
private long getNanosecondTimeStamp()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |