Golang timing the running time of a program segment

Use Go language to calculate the time consumed by a program to run:

Use the time package

import (
    "time"
    "fmt"
)

func function(){
    startTime := time.Now()
    ...
    待计时程序段
    ...
    elapsedTime := time.Since(startTime) / time.Millisecond  // duration in ms
    fmt.Println("Segment finished in %dms", elapsedTime) //Segment finished in xxms
}

startTime can be used multiple times in different positions of the function, that is, the time consumed from starTime to different positions

Guess you like

Origin blog.csdn.net/wwqcherry/article/details/107017912