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.6k Golang : Launching your executable inside a console under Linux
+8.6k Golang : GMail API create and send draft with simple upload attachment example
+8.8k Golang : How to capture return values from goroutines?
+12k Golang : How to display image file or expose CSS, JS files from localhost?
+7.3k Golang : Get YouTube playlist
+19.2k Golang : Get current URL example
+10.9k Golang : How to determine a prime number?
+10.1k Golang : Convert file content to Hex
+20.1k Android Studio : AlertDialog and EditText to get user string input example
+4.4k Mac OSX : Get disk partitions' size, type and name
+4.9k Golang : Issue HTTP commands to server and port example
+9.9k Golang : Use regular expression to get all upper case or lower case characters example