Measure function execution time in golang

Rubin - Jul 12 '20 - - Dev Community

For some reason you may want to keep a track of how much long a function takes to do a certain task in golang, maybe for performance evaluation.Here is a function that tracks the execution time of a complete function call with this one-liner, which logs the result to the standard output.

func timeTrack(start time.Time, name string) {
    elapsed := time.Since(start)
    log.Printf("%s took %s", name, elapsed)
}

Enter fullscreen mode Exit fullscreen mode

The usage is fairly simple and straight forward. Defer the call to this function before doing the actual function call.


package main

import "fmt"

func main() {

defer timeTrack(time.Now(), "Timer")

// your code goes here


}

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .