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
+7.7k Golang : Rot13 and Rot5 algorithms example
+41.5k Golang : How to count duplicate items in slice/array?
+14.3k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+9.1k Golang : GMail API create and send draft with simple upload attachment example
+4.5k Golang : Valued expressions and functions example
+18.8k Golang : Find IP address from string
+15.7k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+9.6k Mac OSX : Get a process/daemon status information
+7.1k Nginx : Password protect a directory/folder
+5.9k Linux/Unix/PHP : Restart PHP-FPM
+9.9k Golang : Find correlation coefficient example
+7k Golang : Get expvar(export variables) to work with multiplexer