Package org.springframework.util

# Class StopWatch

java.lang.Object
org.springframework.util.StopWatch

public class StopWatch extends Object
Simple stop watch, allowing for timing of a number of tasks, exposing total running time and running time for each named task.

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.

Running time is tracked and reported in nanoseconds. As of Spring Framework 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)`.

Since:
  May 2, 2001
Author:
  Rod Johnson, Juergen Hoeller, Sam Brannen
See Also:
  - `start()`
  - `stop()`
  - `shortSummary()`
  - `prettyPrint()`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static final class`
`StopWatch.TaskInfo`
Nested class to hold data about one task executed within the `StopWatch`.

## Constructor Summary

Constructors
Constructor
Description
`StopWatch()`
Construct a new `StopWatch`.
`StopWatch(String id)`
Construct a new `StopWatch` with the given id.

## Method Summary

Modifier and Type
Method
Description
`String`
`currentTaskName()`
Get the name of the currently running task, if any.
`String`
`getId()`
Get the id of this `StopWatch`, as specified on construction.
`StopWatch.TaskInfo`
`getLastTaskInfo()`
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
`String`
`getLastTaskName()`
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
`long`
`getLastTaskTimeMillis()`
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
`long`
`getLastTaskTimeNanos()`
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
`int`
`getTaskCount()`
Get the number of tasks timed.
`StopWatch.TaskInfo[]`
`getTaskInfo()`
Get an array of the data for tasks performed.
`double`
`getTotalTime(TimeUnit timeUnit)`
Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).
`long`
`getTotalTimeMillis()`
Get the total time for all tasks in milliseconds.
`long`
`getTotalTimeNanos()`
Get the total time for all tasks in nanoseconds.
`double`
`getTotalTimeSeconds()`
Get the total time for all tasks in seconds.
`boolean`
`isRunning()`
Determine whether this `StopWatch` is currently running.
`StopWatch.TaskInfo`
`lastTaskInfo()`
Get the last task as a `StopWatch.TaskInfo` object.
`String`
`prettyPrint()`
Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision).
`String`
`prettyPrint(TimeUnit timeUnit)`
Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision).
`void`
`setKeepTaskList(boolean keepTaskList)`
Configure whether the `StopWatch.TaskInfo` array is built over time.
`String`
`shortSummary()`
Get a short description of the total running time in seconds.
`void`
`start()`
Start an unnamed task.
`void`
`start(String taskName)`
Start a named task.
`void`
`stop()`
Stop the current task.
`String`
`toString()`
Generate an informative string describing all tasks performed in seconds.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### StopWatch

public StopWatch()
Construct a new `StopWatch`.

Does not start any task.

### StopWatch

public StopWatch(String id)
Construct a new `StopWatch` with the given id.

The id is handy when we have output from multiple stop watches and need to distinguish between them.

Does not start any task.

Parameters:
  `id` - identifier for this stop watch

## Method Details

### getId

public String getId()
Get the id of this `StopWatch`, as specified on construction.

Returns:
  the id (empty String by default)
Since:
  4.2.2
See Also:
  - `StopWatch(String)`

### setKeepTaskList

public void setKeepTaskList(boolean keepTaskList)
Configure whether the `StopWatch.TaskInfo` array is built over time.

Set this to `false` when using a `StopWatch` for millions of tasks; otherwise, the `TaskInfo` structure will consume excessive memory.

Default is `true`.

### start

public void start() throws IllegalStateException
Start an unnamed task.

The results are undefined if `stop()` or timing methods are called without invoking this method first.

Throws:
  `IllegalStateException`
See Also:
  - `start(String)`
  - `stop()`

### start

public void start(String taskName) throws IllegalStateException
Start a named task.

The results are undefined if `stop()` or timing methods are called without invoking this method first.

Parameters:
  `taskName` - the name of the task to start
Throws:
  `IllegalStateException`
See Also:
  - `start()`
  - `stop()`

### stop

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 `start()` / `stop()` methods.

Throws:
  `IllegalStateException`
See Also:
  - `start()`
  - `start(String)`

### isRunning

public boolean isRunning()
Determine whether this `StopWatch` is currently running.

See Also:
  - `currentTaskName()`

### currentTaskName

@Nullable public String currentTaskName()
Get the name of the currently running task, if any.

Since:
  4.2.2
See Also:
  - `isRunning()`

### lastTaskInfo

public StopWatch.TaskInfo lastTaskInfo() throws IllegalStateException
Get the last task as a `StopWatch.TaskInfo` object.

Throws:
  `IllegalStateException` - if no tasks have run yet
Since:
  6.1

### getLastTaskInfo

@Deprecated(since="6.1") public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
Get the last task as a `StopWatch.TaskInfo` object.

Throws:
  `IllegalStateException`

### getLastTaskName

@Deprecated(since="6.1") public String getLastTaskName() throws IllegalStateException
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
Get the name of the last task.

Throws:
  `IllegalStateException`
See Also:
  - `StopWatch.TaskInfo.getTaskName()`

### getLastTaskTimeNanos

@Deprecated(since="6.1") public long getLastTaskTimeNanos() throws IllegalStateException
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
Get the time taken by the last task in nanoseconds.

Throws:
  `IllegalStateException`
Since:
  5.2
See Also:
  - `StopWatch.TaskInfo.getTimeNanos()`

### getLastTaskTimeMillis

@Deprecated(since="6.1") public long getLastTaskTimeMillis() throws IllegalStateException
Deprecated.
as of 6.1, in favor of `lastTaskInfo()`
Get the time taken by the last task in milliseconds.

Throws:
  `IllegalStateException`
See Also:
  - `StopWatch.TaskInfo.getTimeMillis()`

### getTaskInfo

public StopWatch.TaskInfo[] getTaskInfo()
Get an array of the data for tasks performed.

See Also:
  - `setKeepTaskList(boolean)`

### getTaskCount

public int getTaskCount()
Get the number of tasks timed.

### getTotalTimeNanos

public long getTotalTimeNanos()
Get the total time for all tasks in nanoseconds.

Since:
  5.2
See Also:
  - `getTotalTime(TimeUnit)`

### getTotalTimeMillis

public long getTotalTimeMillis()
Get the total time for all tasks in milliseconds.

See Also:
  - `getTotalTime(TimeUnit)`

### getTotalTimeSeconds

public double getTotalTimeSeconds()
Get the total time for all tasks in seconds.

See Also:
  - `getTotalTime(TimeUnit)`

### getTotalTime

public double getTotalTime(TimeUnit timeUnit)
Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).

Parameters:
  `timeUnit` - the unit to use
Since:
  6.1
See Also:
  - `getTotalTimeNanos()`
  - `getTotalTimeMillis()`
  - `getTotalTimeSeconds()`

### prettyPrint

public String prettyPrint()
Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision).

For custom reporting, call `getTaskInfo()` and use the data directly.

See Also:
  - `prettyPrint(TimeUnit)`
  - `getTotalTimeSeconds()`
  - `StopWatch.TaskInfo.getTimeSeconds()`

### prettyPrint

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 `getTaskInfo()` and use the data directly.

Parameters:
  `timeUnit` - the unit to use for rendering total time and task time
Since:
  6.1
See Also:
  - `prettyPrint()`
  - `getTotalTime(TimeUnit)`
  - `StopWatch.TaskInfo.getTime(TimeUnit)`

### shortSummary

public String shortSummary()
Get a short description of the total running time in seconds.

See Also:
  - `prettyPrint()`
  - `prettyPrint(TimeUnit)`

### toString

public String toString()
Generate an informative string describing all tasks performed in seconds.

Overrides:
  `toString` in class `Object`
See Also:
  - `prettyPrint()`
  - `prettyPrint(TimeUnit)`