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
+14.9k Golang : How to reverse elements order in map ?
+20.4k Golang : Convert string slice to struct and access with reflect example
+6.1k Golang : constant 20013 overflows byte error message
+4.9k Javascript : How to refresh page with JQuery ?
+10.8k Golang : Verify Linux user password again before executing a program example
+6.1k Swift : substringWithRange() function example
+7.9k Golang : Sort lines of text example
+4.5k Golang *File points to a file or directory ?
+6.7k Golang : Convert source code to assembly language
+5.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+12.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+12.9k Golang : Gin framework accept query string by post request example