Skip to content

Instantly share code, notes, and snippets.

@arindamroynitw
Created September 29, 2019 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arindamroynitw/c1cc81da066cfa56ef0034102faf93cb to your computer and use it in GitHub Desktop.
Save arindamroynitw/c1cc81da066cfa56ef0034102faf93cb to your computer and use it in GitHub Desktop.
Efficiently measuring execution time
package main
import (
"fmt"
"time"
)
func measureTime(funcName string) func() {
start := time.Now()
return func() {
fmt.Printf("Time taken by %s function is %v \n", funcName, time.Since(start))
}
}
func expensivePrint() {
defer measureTime("expensivePrint")()
for i := 1; i <= 5; i++ {
fmt.Printf("Current number is %d \n", i)
time.Sleep(100 * time.Millisecond)
}
}
func main() {
expensivePrint()
fmt.Println("Finished executing main")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment