How do you time a function in Go and return its runtime in milliseconds?

前端 未结 5 727
清酒与你
清酒与你 2021-01-31 03:10

How do you time a function in Go and return its runtime in milliseconds?

5条回答
  •  别那么骄傲
    2021-01-31 03:44

    Perhaps you can also use a Duration (elapsed) for this...looks a little bit nicer.

    func trace(s string) (string, time.Time) {
        log.Printf("trace start: %s\n", s)
        return s, time.Now()
    }
    
    func un(s string, startTime time.Time) {
        elapsed := time.Since(startTime)
        log.Printf("trace end: %s, elapsed %f secs\n", s, elapsed.Seconds())
    }
    

提交回复
热议问题