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
+9.8k Golang : Natural string sorting example
+9.2k Golang : Convert file unix timestamp to UTC time example
+5.6k Golang : How to setup a disk space used monitoring service with Telegram bot
+19.8k Golang : For loop continue,break and range
+40.6k Golang : Convert []byte to image
+29.6k error: trying to remove "yum", which is protected
+11.1k Golang : Display list of countries and ISO codes
+3.9k Golang : Calculate a pip value and distance to target profit example
+11.4k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+22.8k Golang : Daemonizing a simple web server process example
+11.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+5.1k Golang : Experimenting with the Rejang script