func measureFuncExecTime() func() {
pc, _, _, _ := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name()
start := time.Now().UnixNano()
return func() {
fmt.Printf("function %s exec cost %+v\n", funcName, time.Duration(time.Now().UnixNano()-start).String())
}
}
func foo() {
defer measureFuncExecTime()()
for i := 1; i <= 10; i++ {
time.Sleep(5 * time.Microsecond)
}
}
func main() {
foo()
}
输出:
image.png
网友评论