自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用 StopWatch 優(yōu)雅打印執(zhí)行耗時

開發(fā) 后端
以后統(tǒng)計代碼執(zhí)行效率建議大家都使用這個工具來進(jìn)行輸出,不需要在starttime、endtime再相減計算,用優(yōu)雅的方式來完成這件事情。

 [[436319]]

0x01:背景

有時在做開發(fā)的時候需要記錄每個任務(wù)執(zhí)行時間,或者記錄一段代碼執(zhí)行時間,最簡單的方法就是打印當(dāng)前時間與執(zhí)行完時間的差值,然后這樣如果執(zhí)行大量測試的話就很麻煩,并且不直觀,如果想對執(zhí)行的時間做進(jìn)一步控制,則需要在程序中很多地方修改,目前spring-framework提供了一個StopWatch類可以做類似任務(wù)執(zhí)行時間控制,也就是封裝了一個對開始時間,結(jié)束時間記錄工具

示例

先來看幾個示例

  •  統(tǒng)計輸出總耗時 
  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 
  •  輸出最后一個任務(wù)的耗時 
  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 
  •  以優(yōu)雅的格式打出所有任務(wù)的耗時以及占比 
  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: 
  •  序列服務(wù)輸出耗時信息 
  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:更多用法

不同的打印結(jié)果

  •  getTotalTimeSeconds() 獲取總耗時秒,同時也有獲取毫秒的方法
  •  prettyPrint() 優(yōu)雅的格式打印結(jié)果,表格形式
  •  shortSummary() 返回簡短的總耗時描述
  •  getTaskCount() 返回統(tǒng)計時間任務(wù)的數(shù)量
  •  getLastTaskInfo().getTaskName() 返回最后一個任務(wù)TaskInfo對象的名稱

更多查看文檔

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

0x03:總結(jié)

以后統(tǒng)計代碼執(zhí)行效率建議大家都使用這個工具來進(jìn)行輸出,不需要在starttime、endtime再相減計算,用優(yōu)雅的方式來完成這件事情。 

 

責(zé)任編輯:龐桂玉 來源: Java知音
相關(guān)推薦

2022-08-03 15:18:41

StopWatch代碼

2015-11-26 10:53:45

LinuxWindowsMac OS

2017-07-26 11:32:50

NETRabbitMQ系統(tǒng)集成

2021-04-20 08:00:31

Redisson關(guān)閉訂單支付系統(tǒng)

2010-09-15 09:03:44

JavaScript

2020-02-05 14:05:21

Java技術(shù)數(shù)組

2024-07-24 08:06:56

2021-11-17 10:25:28

loguru日志Python

2021-08-10 07:41:24

ContextWaitGroupGoroutine

2023-06-28 08:25:14

事務(wù)SQL語句

2023-06-16 09:08:39

ReactContextRFC

2017-04-13 10:14:46

curl命令分析

2011-11-16 17:20:40

HP打印機(jī)

2017-12-14 14:17:08

Windows使用技巧手冊

2021-03-28 09:17:18

JVM場景鉤子函數(shù)

2022-09-14 08:16:48

裝飾器模式對象

2021-01-28 14:53:19

PHP編碼開發(fā)

2024-10-18 08:53:49

SpringMybatis微服務(wù)

2023-10-08 11:09:22

Optional空指針

2025-04-03 09:27:35

JavaScript開發(fā)IIFE
點贊
收藏

51CTO技術(shù)棧公眾號