public final class Timer
extends java.lang.Object
This is used to monitor and report duration of operations in a consistent and standard format. The string output is scaled to the closest time unit. If the time duration is over 1 second, then it is reported in seconds. If the time duration is over 10 milliseconds, then it is reported in milliseconds. If it is less than 10 milliseconds it is reported in nanoseconds.
All values are reported in decimal form, it does not round to the time unit.
Example usage
Timer timer = Timer.createStarted();
do stuff
LOGGER.debug("Time taken by operation was {}",timer.stopAndPrintAsReadableString());
The class supports lap counts for timing internal looping and still reporting overall time.
Example usage
Timer timer = Timer.createStarted();
for (int i=0; i<10; i++) {
do stuff
LOGGER.debug("Time taken inside loop was {}",timer.lapAndPrintAsReadableString());
}
LOGGER.debug("Total time taken by operation was {}",timer.stopAndPrintAsReadableString());
| Modifier and Type | Method and Description |
|---|---|
static Timer |
createStarted()
Creates and returns a Timer which is already started and running.
|
java.time.Duration |
getDuration()
Gets the
Duration between the start and stop times |
java.time.Duration |
getLapDuration()
Returns the
Duration between the start time and the latest lap time. |
java.lang.String |
lapAndPrintAsReadableString()
Generates a lap time and returns the duration between this and the last lap time as a readable string with the
units prepended.
|
void |
start()
Starts the timer, if already started or stopped, will start again from this point in time.
|
java.lang.String |
stopAndPrintAsReadableString()
Stop the timer and return the duration as a readable string with the units prepended.
|
public static Timer createStarted()
Instant class to generate the
current time.public java.lang.String stopAndPrintAsReadableString()
public java.lang.String lapAndPrintAsReadableString()
public java.time.Duration getDuration()
Duration between the start and stop timespublic java.time.Duration getLapDuration()
Duration between the start time and the latest lap time.public void start()