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

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

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

5条回答
  •  甜味超标
    2021-01-31 03:34

    Another easy way can be:

    import (
        "fmt"
        "time"
    )
    
    start := time.Now()
    // some computation
    elapsed := time.Since(start)
    fmt.Println(elapsed)
    

    which will output something like 359.684612ms

提交回复
热议问题