feat: super 包新增 StopWatch 和 StopWatchAndPrintln 函数,用于追踪函数运行时间

This commit is contained in:
kercylan98 2024-02-20 09:28:54 +08:00
parent 40acb567a7
commit 7fa0e68636
1 changed files with 14 additions and 0 deletions

View File

@ -38,3 +38,17 @@ func (slf *LossCounter) String() string {
})
return strings.Join(lines, "\n")
}
// StopWatch 计时器,返回 fn 执行耗时
func StopWatch(fn func()) time.Duration {
start := time.Now()
fn()
return time.Since(start)
}
// StopWatchAndPrintln 计时器,返回 fn 执行耗时,并打印耗时
func StopWatchAndPrintln(name string, fn func()) time.Duration {
loss := StopWatch(fn)
fmt.Println(fmt.Sprintf("%s cost: %s", name, loss.String()))
return loss
}