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

原文地址:https://www.cnblogs.com/lanyangsh/p/8365176.html