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
+5.7k Golang : Markov chains to predict probability of next state example
+11.8k Golang : Convert decimal number(integer) to IPv4 address
+6.9k Golang : Get Alexa ranking data example
+9.2k Golang : How to get username from email address
+19.2k Golang : How to count the number of repeated characters in a string?
+20.3k Nginx + FastCGI + Go Setup.
+26.5k Golang : How to check if a connection to database is still alive ?
+19.4k Golang : Close channel after ticker stopped example
+26.6k Golang : Convert file content into array of bytes
+12.2k Golang : Search and extract certain XML data example
+12.9k Golang : Convert(cast) uintptr to string example
+15.8k Golang : How to reverse elements order in map ?