使用 StopWatch 优雅打印执行耗时

开发 后端
以后统计代码执行效率建议大家都使用这个工具来进行输出,不需要在starttime、endtime再相减计算,用优雅的方式来完成这件事情。

 [[436319]]

0x01:背景

有时在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,然后这样如果执行大量测试的话就很麻烦,并且不直观,如果想对执行的时间做进一步控制,则需要在程序中很多地方修改,目前spring-framework提供了一个StopWatch类可以做类似任务执行时间控制,也就是封装了一个对开始时间,结束时间记录工具

示例

先来看几个示例

  •  统计输出总耗时 
  1. import org.springframework.util.StopWatch;    
  2. public class SpringStopWatchExample { 
  3.     public static void main (String[] args) throws InterruptedException { 
  4.          StopWatch sw = new StopWatch();   
  5.          sw.start();   
  6.          //long task simulation   
  7.         Thread.sleep(1000);    
  8.         sw.stop();    
  9.         System.out.println(sw.getTotalTimeMillis());    
  10.     }    

输出 

  1. 1013 
  •  输出最后一个任务的耗时 
  1. public class SpringStopWatchExample2 {    
  2.       public static void main (String[] args) throws InterruptedException {    
  3.         StopWatch sw = new StopWatch();    
  4.         sw.start("A");//setting a task name    
  5.         //long task simulation    
  6.         Thread.sleep(1000);    
  7.         sw.stop();    
  8.         System.out.println(sw.getLastTaskTimeMillis());    
  9.     }    

输出 

  1. 1009 
  •  以优雅的格式打出所有任务的耗时以及占比 
  1. import org.springframework.util.StopWatch;   
  2.   public class SpringStopWatchExample3 {    
  3.       public static void main (String[] args) throws InterruptedException {   
  4.         StopWatch sw = new StopWatch();    
  5.         sw.start("A");    
  6.         Thread.sleep(500);    
  7.         sw.stop();    
  8.         sw.start("B");    
  9.         Thread.sleep(300);  
  10.         sw.stop();    
  11.         sw.start("C");   
  12.         Thread.sleep(200);    
  13.         sw.stop();    
  14.         System.out.println(sw.prettyPrint());    
  15.     }    

输出 

  1. StopWatch '': running time (millis) = 1031    
  2. -----------------------------------------    
  3. ms     %     Task name    
  4. -----------------------------------------    
  5. 00514  050%  A    
  6. 00302  029%  B    
  7. 00215  021%  C: 
  •  序列服务输出耗时信息 
  1. @Override    
  2. public long nextSeq(String name) {    
  3.     StopWatch watch = new StopWatch();   
  4.     watch.start("单序列获取总消耗");    
  5.     long sequence = generator.generateId(name); 
  6.     watch.stop();    
  7.     logger.info(watch.prettyPrint());    
  8.     return sequence;    

0x02:更多用法

不同的打印结果

  •  getTotalTimeSeconds() 获取总耗时秒,同时也有获取毫秒的方法
  •  prettyPrint() 优雅的格式打印结果,表格形式
  •  shortSummary() 返回简短的总耗时描述
  •  getTaskCount() 返回统计时间任务的数量
  •  getLastTaskInfo().getTaskName() 返回最后一个任务TaskInfo对象的名称

更多查看文档

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StopWatch.html

0x03:总结

以后统计代码执行效率建议大家都使用这个工具来进行输出,不需要在starttime、endtime再相减计算,用优雅的方式来完成这件事情。 

 

责任编辑:庞桂玉 来源: Java知音
相关推荐

2022-08-03 15:18:41

StopWatch代码

2015-11-26 10:53:45

LinuxWindowsMac OS

2017-07-26 11:32:50

NETRabbitMQ系统集成

2021-04-20 08:00:31

Redisson关闭订单支付系统

2010-09-15 09:03:44

JavaScript

2020-02-05 14:05:21

Java技术数组

2017-04-13 10:14:46

curl命令分析

2021-11-17 10:25:28

loguru日志Python

2021-08-10 07:41:24

ContextWaitGroupGoroutine

2023-06-28 08:25:14

事务SQL语句

2023-06-16 09:08:39

ReactContextRFC

2011-11-16 17:20:40

HP打印机

2021-03-28 09:17:18

JVM场景钩子函数

2022-09-14 08:16:48

装饰器模式对象

2017-12-14 14:17:08

Windows使用技巧手册

2021-01-28 14:53:19

PHP编码开发

2023-10-08 11:09:22

Optional空指针

2021-12-18 07:42:15

Ebpf 监控 Node.js

2021-05-30 07:54:24

SPI机制场景

2022-06-02 10:02:47

Kubectl更新应用Linux
点赞
收藏

51CTO技术栈公众号