Golang : calculate elapsed run time
Calculating elapsed time can be useful for profiling or bench marking purpose. In this short tutorial, we will learn how to do simple elapsed time calculation in Go.
Basically, Golang's time.Since()
function should be sufficient :
start := time.Now() // get current time
// do something here
elapsed := time.Since(start)
and here is the full codes below demonstrate how to find out the the elapsed time :
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now() // get current time
// some function
fmt.Println("Hello World!")
elapsed := time.Since(start)
fmt.Printf("Print Hello World took %s\n", elapsed)
}
Output(sample) :
Hello World!
Print Hello World took 46.445us
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.9k Golang : How to call function inside template with template.FuncMap
+4.4k Golang : Get missing location after unmarshal binary and gob decode time.
+12.6k Golang : Delete certain files in a directory
+7.4k PHP : Get coordinates latitude/longitude from string
+9.7k Golang : Change date format to yyyy-mm-dd
+11.6k Golang : How to check if IP address is in range
+6k Golang : How to get username from email address
+12.3k Golang : How to convert(cast) IP address to string?
+6.3k Golang : Find correlation coefficient example
+3.6k Golang : Return multiple values from function
+6.4k Golang : Handle sub domain with Gin
+4.3k Golang : Detect variable or constant type