Conceals use of System#nanoTime(), improving the readability of
application code and reducing the likelihood of calculation errors.
Note that this object is not designed to be thread-safe and does not use synchronization.
This class is normally used to verify performance during proof-of-concept work and in development, rather than as part of production applications.
As of Spring Framework 5.2, running time is tracked and reported in
nanoseconds. As of 6.1, the default time unit for String renderings is
seconds with decimal points in nanosecond precision. Custom renderings with
specific time units can be requested through prettyPrint(TimeUnit).
start(), stop(), shortSummary(), prettyPrint()
| Modifier and Type | Class and Description |
|---|---|
| public static class | StopWatch.
Nested class to hold data about one task executed within the |
| Modifier and Type | Field and Description |
|---|---|
| private String | currentTaskName
Name of the current task. |
| private final String | id
Identifier of this |
| private StopWatch. | |
| private long | startTimeNanos
Start time of the current task. |
| private int | |
| private List | |
| private long | totalTimeNanos
Total running time. |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public String | Returns: the id (empty String by default)Get the id of this |
| public StopWatch. | getLastTaskInfo()
Deprecated
since 6.1. as of 6.1, in favor of
lastTaskInfo()
Get the last task as a |
| public String | getLastTaskName()
Deprecated
since 6.1. as of 6.1, in favor of
lastTaskInfo()
Get the name of the last task. |
| public long | getLastTaskTimeMillis()
Deprecated
since 6.1. as of 6.1, in favor of
lastTaskInfo()
Get the time taken by the last task in milliseconds. |
| public long | getLastTaskTimeNanos()
Deprecated
since 6.1. as of 6.1, in favor of
lastTaskInfo()
Get the time taken by the last task in nanoseconds. |
| public int | |
| public StopWatch. | |
| public double | getTotalTime(TimeUnit
the unit to use timeUnit)Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision). |
| public long | |
| public long | |
| public double | |
| public boolean | |
| public StopWatch. | |
| public String | prettyPrint()
Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision). |
| public String | prettyPrint(TimeUnit
the unit to use for rendering total time and task time timeUnit)Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision). |
| public void | |
| public String | |
| public void | |
| public void | |
| public void | |
| public String | toString()
Overrides java. Generate an informative string describing all tasks performed in seconds. |
| currentTaskName | back to summary |
|---|---|
| private String currentTaskName Name of the current task. | |
| id | back to summary |
|---|---|
| private final String id Identifier of this Handy when we have output from multiple stop watches and need to distinguish between them in log or console output. | |
| lastTaskInfo | back to summary |
|---|---|
| private StopWatch. | |
| startTimeNanos | back to summary |
|---|---|
| private long startTimeNanos Start time of the current task. | |
| taskCount | back to summary |
|---|---|
| private int taskCount | |
| taskList | back to summary |
|---|---|
| private List<StopWatch. | |
| totalTimeNanos | back to summary |
|---|---|
| private long totalTimeNanos Total running time. | |
| StopWatch | back to summary |
|---|---|
| public StopWatch() Construct a new Does not start any task. | |
| StopWatch | back to summary |
|---|---|
| public StopWatch(String id) Construct a new The id is handy when we have output from multiple stop watches and need to distinguish between them. Does not start any task.
| |
| currentTaskName | back to summary |
|---|---|
| public String currentTaskName() Get the name of the currently running task, if any.
| |
| getId | back to summary |
|---|---|
| public String getId() Get the id of this
| |
| getLastTaskInfo | back to summary |
|---|---|
| public StopWatch. Get the last task as a
| |
| getLastTaskName | back to summary |
|---|---|
| public String getLastTaskName() throws IllegalStateException
Get the name of the last task.
| |
| getLastTaskTimeMillis | back to summary |
|---|---|
| public long getLastTaskTimeMillis() throws IllegalStateException
Get the time taken by the last task in milliseconds.
| |
| getLastTaskTimeNanos | back to summary |
|---|---|
| public long getLastTaskTimeNanos() throws IllegalStateException
Get the time taken by the last task in nanoseconds.
| |
| getTaskCount | back to summary |
|---|---|
| public int getTaskCount() Get the number of tasks timed. | |
| getTaskInfo | back to summary |
|---|---|
| public StopWatch. Get an array of the data for tasks performed.
| |
| getTotalTime | back to summary |
|---|---|
| public double getTotalTime(TimeUnit timeUnit) Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).
| |
| getTotalTimeMillis | back to summary |
|---|---|
| public long getTotalTimeMillis() Get the total time for all tasks in milliseconds.
| |
| getTotalTimeNanos | back to summary |
|---|---|
| public long getTotalTimeNanos() Get the total time for all tasks in nanoseconds.
| |
| getTotalTimeSeconds | back to summary |
|---|---|
| public double getTotalTimeSeconds() Get the total time for all tasks in seconds.
| |
| isRunning | back to summary |
|---|---|
| public boolean isRunning() Determine whether this
| |
| lastTaskInfo | back to summary |
|---|---|
| public StopWatch. Get the last task as a
| |
| prettyPrint | back to summary |
|---|---|
| public String prettyPrint() Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision). For custom reporting, call | |
| prettyPrint | back to summary |
|---|---|
| public String prettyPrint(TimeUnit timeUnit) Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision). For custom reporting, call
| |
| setKeepTaskList | back to summary |
|---|---|
| public void setKeepTaskList(boolean keepTaskList) Configure whether the Set this to Default is | |
| shortSummary | back to summary |
|---|---|
| public String shortSummary() Get a short description of the total running time in seconds.
| |
| start | back to summary |
|---|---|
| public void start() throws IllegalStateException Start an unnamed task. The results are undefined if
| |
| start | back to summary |
|---|---|
| public void start(String taskName) throws IllegalStateException Start a named task. The results are undefined if | |
| stop | back to summary |
|---|---|
| public void stop() throws IllegalStateException Stop the current task. The results are undefined if timing methods are called without invoking
at least one pair of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Generate an informative string describing all tasks performed in seconds.
| |
StopWatch.
| Modifier and Type | Field and Description |
|---|---|
| private final String | |
| private final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public double | |
| public long | |
| public long | |
| public double |
| taskName | back to summary |
|---|---|
| private final String taskName | |
| timeNanos | back to summary |
|---|---|
| private final long timeNanos | |
| TaskInfo | back to summary |
|---|---|
| pack-priv TaskInfo(String taskName, long timeNanos) | |
| getTaskName | back to summary |
|---|---|
| public String getTaskName() Get the name of this task. | |
| getTime | back to summary |
|---|---|
| public double getTime(TimeUnit timeUnit) Get the time this task took in the requested time unit (with decimal points in nanosecond precision).
| |
| getTimeMillis | back to summary |
|---|---|
| public long getTimeMillis() Get the time this task took in milliseconds.
| |
| getTimeNanos | back to summary |
|---|---|
| public long getTimeNanos() Get the time this task took in nanoseconds.
| |
| getTimeSeconds | back to summary |
|---|---|
| public double getTimeSeconds() Get the time this task took in seconds.
| |