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.5k Golang : Get local time and equivalent time in different time zone
+31.6k Delete a directory in Go
+6.8k Swift : Convert (cast) String to Double
+8k Golang : How to check if a string with spaces in between is numeric?
+25.7k Golang : Find files by name - cross platform example
+8.6k Golang : Load ASN1 encoded DSA public key PEM file example
+3.7k Linux : sudo yum updates not working
+35.6k Upload multiple files with Go
+7.2k Golang : Check if integer is power of four example
+21.4k Golang : simulate tail -f or read last line from log file example
+7.5k Golang : How to join strings?
+10.7k Golang : Perform sanity checks on filename example