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
+16.8k Golang : How to generate QR codes?
+12.7k Android Studio : Highlight ImageButton when pressed on example
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+10.4k Generate Random number with math/rand in Go
+7.1k Golang : Gorrila mux.Vars() function example
+7.4k Golang : Scanf function weird error in Windows
+25.9k Golang : How to read integer value from standard input ?
+29.4k Golang : Saving(serializing) and reading file with GOB
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+5.9k Golang : Detect variable or constant type
+5.8k Javascript : How to replace HTML inside <div>?
+13.4k Golang : error parsing regexp: invalid or unsupported Perl syntax