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
+22.9k Golang : Get ASCII code from a key press(cross-platform) example
+5.4k Javascript : How to refresh page with JQuery ?
+16.1k Golang :Trim white spaces from a string
+8.5k Golang : Heap sort example
+11.1k Golang : Concatenate (combine) buffer data example
+10.3k Golang : Resolve domain name to IP4 and IP6 addresses.
+14k Golang : Get uploaded file name or access uploaded files
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+20.9k Golang : Create and resolve(read) symbolic links
+4.6k Python : Find out the variable type and determine the type with simple test
+10.4k Golang : Simple File Server
+18.8k Golang : Clearing slice