計時器之StopWatch

2022-03-08 04:53:25 字數 1933 閱讀 8049

stopwatchorg.springframework.util包下的乙個工具類,通過它可方便的對程式執行時間進行統計。

public class stopwatchtest 

}

控制台輸出:

stopwatch '': running time = 10659200 ns

---------------------------------------------

ns % task name

---------------------------------------------

003353500 031% task_01

002327200 022% task_02

004978500 047% task_03

提供了兩種建構函式用於初始化stopwatch

// 用於區分秒錶的主鍵

private final string id;

// 預設主鍵為空

public stopwatch()

// 指定秒錶主鍵

public stopwatch(string id)

提供兩種啟動方式,當需要統計多個任務時,需要指定任務名稱

// 當前任務名

private string currenttaskname;

// 統計開始時間

private long starttimenanos;

public void start() throws illegalstateexception

public void start(string taskname) throws illegalstateexception

// 設定當前統計任務名稱

this.currenttaskname = taskname;

// 記錄當前時間

this.starttimenanos = system.nanotime();

}

// 執行的任務個數

private int taskcount;

// 總執行時間

private long totaltimenanos;

// 是否保留任務資訊

private boolean keeptasklist = true;

// 最近一次執行的任務資訊

private taskinfo lasttaskinfo;

// 執行的任務清單列表

private final listtasklist = new linkedlist<>();

public void stop() throws illegalstateexception

long lasttime = system.nanotime() - this.starttimenanos;

this.totaltimenanos += lasttime;

this.lasttaskinfo = new taskinfo(this.currenttaskname, lasttime);

if (this.keeptasklist)

++this.taskcount;

this.currenttaskname = null;

}

public static final class taskinfo
public string prettyprint() 

else

} return sb.tostring();

}

Stopwatch計時器 秒錶 C

net2.0也提供了這樣乙個秒錶 stopwatch類,它可以比較精確地測量時間。速度測試 軟體的效能和可測性是乙個複雜的主題。要確保應用程式能夠滿足使用者的期望,就需要在開發周期內考慮它的效能和可測性。這在設計階段至關重要,乙個糟糕的設計幾乎肯定會導致糟糕的使用者體驗。然而,僅僅有好的設計也不能保...

Stopwatch計時器 秒錶 C

net2.0也提供了這樣乙個秒錶 stopwatch類,它可以比較精確地測量時間。速度測試 軟體的效能和可測性是乙個複雜的主題。要確保應用程式能夠滿足使用者的期望,就需要在開發周期內考慮它的效能和可測性。這在設計階段至關重要,乙個糟糕的設計幾乎肯定會導致糟糕的使用者體驗。然而,僅僅有好的設計也不能保...

stopwatch計時器統計程式耗時

1 引入依賴 import com.google.common.base.stopwatch 2 基礎用法 stopwatch stopwatch stopwatch.createstarted 建立計時器並開始計時 dosomething log.info dosomething 耗時 stopw...