Skip to content

Commit

Permalink
feat: super 包新增 StopWatch 和 StopWatchAndPrintln 函数,用于追踪函数运行时间
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Feb 20, 2024
1 parent 40acb56 commit 7fa0e68
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions utils/super/loss_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7fa0e68

Please sign in to comment.