public class Stopwatch extends Object
A convenient timing object that supports multiple counter types.
Use the default constructor, with no arguments, to create a stopwatch with all of the available metrics. If required, an additional constructor may be used to provide a stopwatch with specific counters. E.g.:
new Stopwatch(); // provides all available counter types new Stopwatch(Counter.Type.WALL_CLOCK_TIME); // wall-clock time only new Stopwatch(Counter.Type.CPU_TIME, Counter.Type.USER_TIME); // two counters
Note: For a list of available counters, refer to Counter.Type.
Call start() to start the timing session. When you are done, call
stop() to complete the timing session.
Hint: A single call to the factory method Stopwatch.createStarted() may
create a started stopwatch for convenience.
Use elapsedTime(Counter.Type) to retrieve the elapsed time for a particular
counter. E.g.:
double elapsedTimeNanos = cpuTime.elapsedTime(Counter.Type.CPU_TIME, TimeUnit.NANOSECONDS);
Use the output method printStatistics(System.out) to print stopwatch statistics
to the system console.
Although it is intended that the output methods printStatistics() should only
be called after the stop, some suitable, temporary data may be returned if the
stopwatch is still running. In this scenario, the initial values will be compared to
the most up-to-date ones, retrieved at the moment of the call. The same applies to the
elapsedTime() methods available for each counter instance.
Note: This class is not thread-safe. In a multi-thread context, different instances must be created for each thread.
Counter,
Counter.Type| Constructor and Description |
|---|
Stopwatch()
Creates a new stopwatch with default counter types.
|
Stopwatch(Counter.Type... types)
Creates a new stopwatch with specific counter types.
|
| Modifier and Type | Method and Description |
|---|---|
static Stopwatch |
createStarted()
Provides a started stopwatch for convenience with default counter types.
|
static Stopwatch |
createStarted(Counter.Type... types)
Provides a started stopwatch for convenience with specific counter types.
|
Duration |
elapsedTime(Counter.Type type)
A convenient method that returns the elapsed time of a specific counter.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit)
A convenient method that returns the elapsed time of a specific counter, in the
specified time unit.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit,
ConversionMode conversionMode)
A convenient method that returns the elapsed time of a specific counter, in the
specified time unit, by applying a custom
ConversionMode. |
Counter |
getCounter(Counter.Type type)
Returns the counter instance associated with a given type in this stopwatch.
|
Collection<Counter> |
getCounters()
Returns the counters associated with this stopwatch instance.
|
boolean |
isStarted()
Returns
true if this stopwatch is started. |
void |
printStatistics(PrintStream printStream)
Prints stopwatch statistics in the specified print stream.
|
void |
printStatistics(PrintStream printStream,
TimeUnit timeUnit)
Prints stopwatch statistics in the specified print stream, with a custom time unit.
|
void |
reset()
Resets all counters associated with this stopwatch instance.
|
void |
start()
Starts the timing session.
|
void |
stop()
Stops the timing session.
|
public Stopwatch()
public Stopwatch(Counter.Type... types)
types - the types to be setpublic static Stopwatch createStarted()
public static Stopwatch createStarted(Counter.Type... types)
types - the types to be setpublic void reset()
public void start()
IllegalStateException - if the stopwatch state is not suitable for this actionpublic void stop()
IllegalStateException - if the stopwatch state is not suitable for this actionpublic boolean isStarted()
true if this stopwatch is started.public Collection<Counter> getCounters()
public Counter getCounter(Counter.Type type)
type - the counter type to be fetchedIllegalArgumentException - if the specified type is not available in this
stopwatch instancepublic Duration elapsedTime(Counter.Type type)
This has the same effect as calling: stopwatch.getCounter(type).elapsedTime()
type - the counter type to be fetchedIllegalArgumentException - if the specified type is not available in this
stopwatch instancepublic double elapsedTime(Counter.Type type, TimeUnit timeUnit)
This has the same effect as calling:
stopwatch.getCounter(type).elapsedTime(timeUnit)
type - the counter type to be fetchedtimeUnit - the time unit to which the elapsed time will be convertedIllegalArgumentException - if the specified type is not available in this
stopwatch instancepublic double elapsedTime(Counter.Type type, TimeUnit timeUnit, ConversionMode conversionMode)
ConversionMode.
This has the same effect as calling:
stopwatch.getCounter(type).elapsedTime(timeUnit, conversionMode)
type - the counter type to be fetchedtimeUnit - the time unit to which the elapsed time will be convertedconversionMode - the ConversionMode to be appliedIllegalArgumentException - if the specified type is not available in this
stopwatch instancepublic void printStatistics(PrintStream printStream)
printStream - the print stream to which statistics will be sentpublic void printStatistics(PrintStream printStream, TimeUnit timeUnit)
printStream - the print stream to which statistics will be senttimeUnit - the time unit for the elapsed times to be displayedCopyright © 2020. All rights reserved.