public class Stopwatch extends Object
A convenient timing object that supports multiple counter types and timing sessions.
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 total elapsed time for a
particular counter type. E.g.:
Duration cpuTime = stopwatch.elapsedTime(Counter.Type.CPU_TIME);
Use elapsedTime(Counter.Type, TimeUnit) to retrieve the total elapsed time
converted into a specific time unit. E.g.:
double cpuTimeNanos = stopwatch.elapsedTime(Counter.Type.CPU_TIME, TimeUnit.NANOSECONDS);
Hint: If the stopwatch was created with only one counter type, then no argument is needed to retrieve the total elapsed time. E.g.:
Duration elapsedTime = stopwatch.elapsedTime();
Use the output methods printSummary(System.out) and
printDetails(System.out) to print stopwatch statistics to the system console.
Although it is intended that the output methods elapsedTime(),
printSummary(), and printDetails() should be called after the stop,
some suitable, temporary data may be returned if the current timing session 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.
Note: This class is not thread-safe. In a multi-thread context, different instances must be created for each thread.
Counter,
Counter.Type,
TimingSession| 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 with default counter types, for convenience.
|
static Stopwatch |
createStarted(Counter.Type... types)
Provides a started stopwatch with specific counter types, for convenience.
|
Duration |
elapsedTime()
Returns the total elapsed time for a single counter type, provided that this stopwatch
is keeping a single type.
|
Duration |
elapsedTime(Counter.Type type)
Returns the total elapsed time for a specific counter.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit)
Returns the total elapsed time for a specific counter, in the specified time unit.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit,
ConversionMode conversionMode)
Returns the total elapsed time for a specific counter, in the specified time unit, with
a custom
ConversionMode applied. |
double |
elapsedTime(TimeUnit timeUnit)
Returns the total elapsed time in the specified time unit for a single counter type,
provided that this stopwatch is keeping a single type.
|
double |
elapsedTime(TimeUnit timeUnit,
ConversionMode conversionMode)
Returns the total elapsed time in the specified time unit for a single counter type,
provided that this stopwatch is keeping a single type.
|
Map<Counter.Type,List<Counter>> |
getAllCountersByType()
Returns a map of populated counters grouped by type, where each entry in the counters
list represents a timing session.
|
List<Counter> |
getCounters(Counter.Type type)
Returns a list of populated counters for a specific type in this stopwatch, or an empty
list if no counter is found (for example, if the stopwatch was not yet started, or
after reset).
|
List<Counter.Type> |
getTypes()
Returns the counter types associated with this stopwatch instance.
|
boolean |
isStarted()
Returns
true if this stopwatch is started. |
void |
printDetails(PrintStream printStream)
Prints detailed information about timing sessions in the specified print stream.
|
void |
printDetails(PrintStream printStream,
PrintStyle printStyle)
Prints detailed information about timing sessions in the specified print stream, with a
custom
PrintStyle. |
void |
printSummary(PrintStream printStream)
Prints summarized elapsed times in the specified print stream.
|
void |
printSummary(PrintStream printStream,
PrintStyle printStyle)
Prints summarized elapsed times in the specified print stream, with a custom
PrintStyle. |
void |
reset()
Cleans all timing sessions in this stopwatch.
|
void |
start()
Starts a new timing session.
|
void |
stop()
Stops the current timing session.
|
String |
toString()
Returns a string containing a formatted stopwatch summary.
|
public Stopwatch()
public Stopwatch(Counter.Type... types)
If no type is specified, then all of the available types will be maintained.
types - the types to be setpublic static Stopwatch createStarted()
public static Stopwatch createStarted(Counter.Type... types)
If no type is specified, then all of the available types will be maintained.
types - the types to be setpublic void reset()
public void start()
public void stop()
IllegalStateException - if the stopwatch state is not suitable for this actionpublic boolean isStarted()
true if this stopwatch is started.public List<Counter.Type> getTypes()
public Map<Counter.Type,List<Counter>> getAllCountersByType()
public List<Counter> getCounters(Counter.Type type)
type - the counter type to be fetchedIllegalArgumentException - if the specified type was not assigned to the
stopwatch during instantiationpublic Duration elapsedTime()
IllegalStateException - if the stopwatch is keeping more than one counter typepublic double elapsedTime(TimeUnit timeUnit)
timeUnit - the time unit to which the elapsed time will be convertedIllegalStateException - if the stopwatch is keeping more than one counter typepublic double elapsedTime(TimeUnit timeUnit, ConversionMode conversionMode)
timeUnit - the time unit to which the elapsed time will be convertedconversionMode - the ConversionMode to be appliedIllegalStateException - if the stopwatch is keeping more than one counter typepublic Duration elapsedTime(Counter.Type type)
type - the counter type to be fetchedIllegalArgumentException - if the specified type was not assigned to the
stopwatch during instantiationpublic double elapsedTime(Counter.Type type, TimeUnit timeUnit)
type - the counter type to be fetchedtimeUnit - the time unit to which the elapsed time will be convertedIllegalArgumentException - if the specified type was not assigned to the
stopwatch during instantiationpublic double elapsedTime(Counter.Type type, TimeUnit timeUnit, ConversionMode conversionMode)
ConversionMode applied.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 was not assigned to the
stopwatch during instantiationpublic void printSummary(PrintStream printStream)
printStream - the print stream to which data will be sentNullPointerException - if the PrintStream is nullpublic void printSummary(PrintStream printStream, PrintStyle printStyle)
PrintStyle.printStream - the print stream to which data will be sentprintStyle - the PrintStyle to be appliedNullPointerException - if the PrintStream is nullIllegalArgumentException - if the specified PrintStyle is not compatible with
PrintFormat.SUMMARIZEDpublic void printDetails(PrintStream printStream)
printStream - the print stream to which information will be sentNullPointerException - if the PrintStream is nullpublic void printDetails(PrintStream printStream, PrintStyle printStyle)
PrintStyle.printStream - the print stream to which information will be sentprintStyle - the PrintStyle to be appliedNullPointerException - if the PrintStream is nullIllegalArgumentException - if the specified PrintStyle is not compatible with
PrintFormat.DETAILEDCopyright © 2021. All rights reserved.