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
+8k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+15.1k Golang : Check if a string contains multiple sub-strings in []string?
+9.1k Golang : Read file and convert content to string
+5.4k CodeIgniter : form input set_value cause " to become & quot
+17.3k Golang : Example for RSA package functions
+22.9k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+10.8k Golang : Perform sanity checks on filename example
+17.9k Golang : Clearing slice
+5.6k Elasticsearch : Shutdown a local node
+5.8k Golang : Muxing with Martini example
+11.4k Golang : HTTP response JSON encoded data
+11.1k Golang : Display list of countries and ISO codes