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
+10.2k Golang : Print how to use flag for your application example
+35.2k Golang : Upload and download file to/from AWS S3
+9.3k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+11.6k CodeIgniter : Import Linkedin data
+19.3k Golang : Execute shell command
+13.7k Golang : reCAPTCHA example
+14.2k Javascript : Prompt confirmation before exit
+12.6k Golang : Forwarding a local port to a remote server example
+18.3k Golang : Get command line arguments
+7.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+7.8k Golang : Scan files for certain pattern and rename part of the files
+6.9k Golang : Muxing with Martini example