Class StopWatch
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classNested class to hold data about one task executed within theStopWatch. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet the name of the currently running task, if any.getId()Get the id of thisStopWatch, as specified on construction.Deprecated.Deprecated.as of 6.1, in favor oflastTaskInfo()longDeprecated.as of 6.1, in favor oflastTaskInfo()longDeprecated.as of 6.1, in favor oflastTaskInfo()intGet the number of tasks timed.Get an array of the data for tasks performed.doublegetTotalTime(TimeUnit timeUnit) Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).longGet the total time for all tasks in milliseconds.longGet the total time for all tasks in nanoseconds.doubleGet the total time for all tasks in seconds.booleanDetermine whether thisStopWatchis currently running.Get the last task as aStopWatch.TaskInfoobject.Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision).prettyPrint(TimeUnit timeUnit) Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision).voidsetKeepTaskList(boolean keepTaskList) Configure whether theStopWatch.TaskInfoarray is built over time.Get a short description of the total running time in seconds.voidstart()Start an unnamed task.voidStart a named task.voidstop()Stop the current task.toString()Generate an informative string describing all tasks performed in seconds.
-
Constructor Details
-
StopWatch
public StopWatch()Construct a newStopWatch.Does not start any task.
-
StopWatch
Construct a newStopWatchwith 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
Get the id of thisStopWatch, as specified on construction.- Returns:
- the id (empty String by default)
- Since:
- 4.2.2
- See Also:
-
setKeepTaskList
public void setKeepTaskList(boolean keepTaskList) Configure whether theStopWatch.TaskInfoarray is built over time.Set this to
falsewhen using aStopWatchfor millions of tasks; otherwise, theTaskInfostructure will consume excessive memory.Default is
true. -
start
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
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:
-
stop
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:
-
isRunning
public boolean isRunning()Determine whether thisStopWatchis currently running.- See Also:
-
currentTaskName
-
lastTaskInfo
Get the last task as aStopWatch.TaskInfoobject.- Throws:
IllegalStateException- if no tasks have run yet- Since:
- 6.1
-
getLastTaskInfo
Deprecated.as of 6.1, in favor oflastTaskInfo()Get the last task as aStopWatch.TaskInfoobject.- Throws:
IllegalStateException
-
getLastTaskName
Deprecated.as of 6.1, in favor oflastTaskInfo()Get the name of the last task.- Throws:
IllegalStateException- See Also:
-
getLastTaskTimeNanos
Deprecated.as of 6.1, in favor oflastTaskInfo()Get the time taken by the last task in nanoseconds.- Throws:
IllegalStateException- Since:
- 5.2
- See Also:
-
getLastTaskTimeMillis
Deprecated.as of 6.1, in favor oflastTaskInfo()Get the time taken by the last task in milliseconds.- Throws:
IllegalStateException- See Also:
-
getTaskInfo
-
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:
-
getTotalTimeMillis
public long getTotalTimeMillis()Get the total time for all tasks in milliseconds.- See Also:
-
getTotalTimeSeconds
public double getTotalTimeSeconds()Get the total time for all tasks in seconds.- See Also:
-
getTotalTime
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:
-
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
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:
-
shortSummary
Get a short description of the total running time in seconds.- See Also:
-
toString
-
lastTaskInfo()