鴻蒙OS Stopwatch

2022-09-01 10:21 更新

Stopwatch

java.lang.Object

|---ohos.utils.Stopwatch

  1. public final class Stopwatch
  2. extends Object

提供秒表功能來記錄從主任務(wù)拆分出來的子任務(wù)的執(zhí)行情況,并使用 HiLog 將記錄保存在調(diào)試級日志中。

示例代碼:

  1. Stopwatch stopwatch = new Stopwatch(label, "main task");
  2. // Records the completion time of subtask A split from the main task.
  3. stopwatch.split("sub task A");
  4. // Records the completion time of subtask B split from the main task.
  5. stopwatch.split("sub task B");
  6. // Records the completion time of subtask C split from the main task.
  7. stopwatch.split("sub task C");
  8. stopwatch.writeLog();

以下記錄被寫入日志:

  1. 8186 D 00000/StopwatchDemo: main task: begin
  2. 8186 D 00000/StopwatchDemo: main task: 11 ms, 11%, sub task A
  3. 8186 D 00000/StopwatchDemo: main task: 76 ms, 76%, sub task B
  4. 8186 D 00000/StopwatchDemo: main task: 13 ms, 13%, sub task C
  5. 8186 D 00000/StopwatchDemo: main task: end 100 ms

構(gòu)造函數(shù)總結(jié)

構(gòu)造函數(shù) 描述
Stopwatch(HiLogLabel label, String taskName) 創(chuàng)建秒表記錄器。

方法總結(jié)

修飾符和類型 方法 描述
void reset() 重置秒表記錄器。
void split(String subtaskName) 記錄從主任務(wù)拆分出來的子任務(wù)的完成時間。
void writeLog() 將秒表記錄器中的記錄寫入 HiLog 中定義的調(diào)試級別日志。
從類 java.lang.Object 繼承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

構(gòu)造函數(shù)詳細(xì)信息

Stopwatch

public Stopwatch(HiLogLabel label, String taskName)

創(chuàng)建秒表記錄器。

建議您為每個主要任務(wù)創(chuàng)建一個秒表記錄器。

參數(shù):

參數(shù)名稱 參數(shù)描述
label 表示 HiLog 標(biāo)簽。
taskName 表示任務(wù)名稱。

方法詳情

split

public void split(String subtaskName)

記錄從主任務(wù)拆分出來的子任務(wù)的完成時間。

該方法在子任務(wù)執(zhí)行后調(diào)用。 Stopwatch#writeLog() 使用記錄的時間來計算子任務(wù)執(zhí)行的持續(xù)時間。

參數(shù):

參數(shù)名稱 參數(shù)描述
subtaskName 表示子任務(wù)名稱。

reset

public void reset()

重置秒表記錄器。

如果主任務(wù)需要重復(fù)執(zhí)行,調(diào)用此方法重置秒表記錄器。 重置后,所有歷史記錄將被清除。

writeLog

public void writeLog()

將秒表記錄器中的記錄寫入 HiLog 中定義的調(diào)試級別日志。

注意:此方法不會清除歷史記錄,歷史記錄也會寫入日志。 要清除歷史記錄,請調(diào)用 Stopwatch#reset()。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號