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
+28.5k Golang : How to verify uploaded file is image or allowed file types
+14.2k Golang : Delete certain files in a directory
+13.6k Golang : Accurate and reliable decimal calculations
+7.9k Golang : Get SPF and DMARC from email headers to fight spam
+10.6k Golang : Handle API query by curl with Gorilla Queries example
+26.9k Golang : Connect to database (MySQL/MariaDB) server
+14.4k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+15.1k Golang : Check if a string contains multiple sub-strings in []string?
+9.6k Golang : Roll the dice example
+6.5k Golang : Command line ticker to show work in progress
+7.3k Golang : How to check if input string is a word?
+18.3k Golang : How to Set or Add Header http.ResponseWriter?