我想到了使用defer语句来测量 Go 中程序的运行时间:func main() { start := time.Now() defer fmt.Println(fmt.Sprintf("The process took %s", time.Now().Sub(start))) ...}我认为这会产生正确的结果,但事实并非如此:[09:36:55]pc@work:~/test$ go run ./main.goProcessed 209806/209806 TUF filesAll records are inserted in the database.Verifying records are inserted correctly...Verified 209806/209806 TUF filesAll records have been inserted and verified successfully.The process took 600ns[14:24:06]pc@work:~/test$尽管该过程耗时 5 小时(从左侧的时间戳可以看出),但延迟fmt.Println()语句显示为 600 纳秒。我究竟做错了什么?
- 2 回答
- 0 关注
- 90 浏览
添加回答
举报
0/150
提交
取消