【go】golang 如何查看程序执行消耗时间

写代码过程中,有时需要分析代码块的时间消耗。
本文介绍使用time包中的Since函数查看程序执行时间。

package main



import (
        "fmt"
        "time"
)


func main() {

        t := time.Now()

        fmt.Println("Hello")

        for i:=0; i< 10002; i++ {
                fmt.Println(i)
        }

        elapsed := time.Since(t)

        fmt.Println("app elapsed:", elapsed)
}

Since返回从时间t开始,到执行Since经历的时间。

output:

0
...
9999
10000
10001
app elapsed: 20ms

 

posted @ 2022-10-17 10:31  opensmarty  阅读(224)  评论(0编辑  收藏  举报