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.
As of Spring Framework 5.2, running time is tracked and reported in nanoseconds.
- Since:
- May 2, 2001
- Author:
- Rod Johnson, Juergen Hoeller, Sam Brannen
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic final classNested class to hold data about one task executed within theStopWatch.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionGet the name of the currently running task, if any.getId()Get the id of thisStopWatch, as specified on construction.Get the last task as aStopWatch.TaskInfoobject.Get the name of the last task.longGet the time taken by the last task in milliseconds.longGet the time taken by the last task in nanoseconds.intGet the number of tasks timed.Get an array of the data for tasks performed.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.Generate a string with a table describing all tasks performed.voidsetKeepTaskList(boolean keepTaskList) Configure whether theStopWatch.TaskInfoarray is built over time.Get a short description of the total running time.voidstart()Start an unnamed task.voidStart a named task.voidstop()Stop the current task.toString()Generate an informative string describing all tasks performed
- 
Constructor Details- 
StopWatchpublic StopWatch()Construct a newStopWatch.Does not start any task. 
- 
StopWatchConstruct 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- 
getIdGet the id of thisStopWatch, as specified on construction.- Returns:
- the id (empty String by default)
- Since:
- 4.2.2
- See Also:
 
- 
setKeepTaskListpublic 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.
- 
startStart an unnamed task.The results are undefined if stop()or timing methods are called without invoking this method first.- Throws:
- IllegalStateException
- See Also:
 
- 
startStart 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:
 
- 
stopStop 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:
 
- 
isRunningpublic boolean isRunning()Determine whether thisStopWatchis currently running.- See Also:
 
- 
currentTaskNameGet the name of the currently running task, if any.- Since:
- 4.2.2
- See Also:
 
- 
getLastTaskTimeNanosGet the time taken by the last task in nanoseconds.- Throws:
- IllegalStateException
- Since:
- 5.2
- See Also:
 
- 
getLastTaskTimeMillisGet the time taken by the last task in milliseconds.- Throws:
- IllegalStateException
- See Also:
 
- 
getLastTaskNameGet the name of the last task.- Throws:
- IllegalStateException
 
- 
getLastTaskInfoGet the last task as aStopWatch.TaskInfoobject.- Throws:
- IllegalStateException
 
- 
getTotalTimeNanospublic long getTotalTimeNanos()Get the total time for all tasks in nanoseconds.- Since:
- 5.2
- See Also:
 
- 
getTotalTimeMillispublic long getTotalTimeMillis()Get the total time for all tasks in milliseconds.- See Also:
 
- 
getTotalTimeSecondspublic double getTotalTimeSeconds()Get the total time for all tasks in seconds.- See Also:
 
- 
getTaskCountpublic int getTaskCount()Get the number of tasks timed.
- 
getTaskInfoGet an array of the data for tasks performed.
- 
shortSummaryGet a short description of the total running time.
- 
prettyPrintGenerate a string with a table describing all tasks performed.For custom reporting, call getTaskInfo()and use the task info directly.
- 
toStringGenerate an informative string describing all tasks performedFor custom reporting, call getTaskInfo()and use the task info directly.
 
-