Golang Measuring Running Time

Page content

When coding in any language, more often than not, you might be interested to see how long a particular function or whole program takes to execute. This can be done in one of the two ways in for a Go program

To see how long a program takes:

time go run src/main.go Not specific to GO but this easier measure any cli command.

To see how long a particular snippet takes:

func main() {
  startedAt := time.Now()
  log.Printf("Started at %s", startedAt)

  replaceYourFunctionName()

  finishedAt := time.Now()
  log.Printf("Finished at: %s, Took: %s", finishedAt, finishedAt.Sub(startedAt))
}