Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use StopWatch gracefully instead of currentTimeMillis computing program to execute time-consuming

Shulou Source: shulou.com Published: 2022-06-03 13:46:10 09月17日 Update

This article will explain in detail how to use StopWatch to elegantly replace currentTimeMillis calculation program execution time, Xiaobian thinks it is quite practical, so share it with you for a reference, I hope you can gain something after reading this article.

demand

Sometimes it is necessary to record the execution time of the program. The simplest way is to print the difference between the current time and the execution time. The disadvantages are:

It's a lot of trouble to run a lot of tests.

not intuitive

If you want to have more control over execution time, you need to modify many places in the program

So Spring provides a StopWatch class that can do similar task execution time control, that is, encapsulates a tool for recording start time and end time.

case

Total time spent counting outputs

import org.springframework.util.StopWatch; public class SpringStopWatchExample { public static void main (String[] args) throws InterruptedException { StopWatch sw = new StopWatch(); sw.start(); //long task simulation Thread.sleep(1000); sw.stop(); System.out.println(sw.getTotalTimeMillis()); public class SpringStopWatchExample2 { public static void main (String[] args) throws InterruptedException { StopWatch sw = new StopWatch(); sw.start("A");//setting a task name //long task simulation Thread.sleep(1000); sw.stop(); System.out.println(sw.getLastTaskTimeMillis()); import org.springframework.util.StopWatch; public class SpringStopWatchExample3 { public static void main (String[] args) throws InterruptedException { StopWatch sw = new StopWatch(); sw.start("A"); Thread.sleep(500); sw.stop(); sw.start("B"); Thread.sleep(300); sw.stop(); sw.start("C"); Thread.sleep(200); sw.stop(); System.out.println(sw.prettyPrint()); }} Sequence service output time information @Overridepublic long nextSeq(String name) { StopWatch watch = new StopWatch(); watch.start("Single sequence acquisition total consumption"); long sequence = generator.generateId(name); watch.stop(); logger.info(watch.prettyPrint()); return sequence;}

getTotalTimeSeconds() Gets the total elapsed time in seconds, but there are also ways to get milliseconds

prettyPrint() Elegant format print results, tabular form

shortSummary() Returns a short description of the total elapsed time

getTaskCount() Returns the number of time counting tasks

getTaskInfo ().getTaskName() Returns the name of the last Task TaskInfo object

About "how to use StopWatch elegant alternative currentTimeMillis calculation program execution time" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Time task elegance program article output sequence more format control statistics good practical short intuitive information content at the same time name place Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Redmi macOS Apple