Background
Sometimes we need to record the execution time of each task when doing development, or record the execution time of a piece of code, the simplest way is to print the difference between the current time and the execution time, and then if the implementation of a large number of tests is very troublesome, and not intuitive, if you want to do further control of the execution time, it is necessary to modify many places in the program. Currently spring-framework provides a StopWatch class can do similar task execution time control, that is, encapsulated a start time, end time record tool.
Examples
Let’s look at some examples
Total time spent
|
|
Time consumption of the last task
|
|
Output the time spent on all tasks in an elegant format and as a percentage
|
|
More Usage
Different print results
getTotalTimeSeconds()
gets the total elapsed time in seconds, but also has a method to get millisecondsprettyPrint()
prints the results in an elegant format, in a tableshortSummary()
return a short description of the total elapsed timegetTaskCount()
return the number of statistical time tasksgetLastTaskInfo().getTaskName()
returns the name of the last task TaskInfo object
See more at Documentation
Summary
It is recommended that everyone use this elegant way to count the efficiency of code execution.
Reference https://ningyu1.github.io/20190505/116-stop-watch.html