Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report