From 7fa0e6863613bbd137be98fa5f4d57345622e0c2 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Tue, 20 Feb 2024 09:28:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20super=20=E5=8C=85=E6=96=B0=E5=A2=9E=20S?= =?UTF-8?q?topWatch=20=E5=92=8C=20StopWatchAndPrintln=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E8=BF=BD=E8=B8=AA=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/super/loss_counter.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/utils/super/loss_counter.go b/utils/super/loss_counter.go index 8cfc5e5..56aec47 100644 --- a/utils/super/loss_counter.go +++ b/utils/super/loss_counter.go @@ -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 +}