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
+31.3k Golang : Strip slashes from string example
+3.8k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+7.7k Golang : Accessing content anonymously with Tor
+60.1k Golang : Convert HTTP Response body to string
+7.7k Facebook : Getting the friends list with PHP return JSON format
+5.2k Golang : Pat multiplexer routing example
+7.4k Golang : Populate or initialize struct with values example
+15.4k Golang : Get all upper case or lower case characters from string example
+6.6k Golang : Convert word to its plural form example
+42.9k Golang : Convert int to byte array([]byte)
+4.5k PageSpeed : Clear or flush cache on web server
+9.7k Golang : Print UTF-8 fonts on image example