鴻蒙OS Stopwatch

2022-09-01 10:21 更新

Stopwatch

java.lang.Object

|---ohos.utils.Stopwatch

public final class Stopwatch
extends Object

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

示例代碼:

 Stopwatch stopwatch = new Stopwatch(label, "main task");
 // Records the completion time of subtask A split from the main task.
 stopwatch.split("sub task A");
 // Records the completion time of subtask B split from the main task.
 stopwatch.split("sub task B");
 // Records the completion time of subtask C split from the main task.
 stopwatch.split("sub task C");
 stopwatch.writeLog();

以下記錄被寫入日志:

 8186 D 00000/StopwatchDemo: main task: begin
 8186 D 00000/StopwatchDemo: main task:      11 ms, 11%, sub task A
 8186 D 00000/StopwatchDemo: main task:      76 ms, 76%, sub task B
 8186 D 00000/StopwatchDemo: main task:      13 ms, 13%, sub task C
 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ù)的完成時(shí)間。
void writeLog() 將秒表記錄器中的記錄寫入 HiLog 中定義的調(diào)試級(jí)別日志。
從類 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)建秒表記錄器。

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

參數(shù):

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

方法詳情

split

public void split(String subtaskName)

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

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

參數(shù):

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

reset

public void reset()

重置秒表記錄器。

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

writeLog

public void writeLog()

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

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

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)