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
+9.5k Golang : Detect number of active displays and the display's resolution
+17.6k Golang : Read data from config file and assign to variables
+7.9k Golang : Handle Palindrome string with case sensitivity and unicode
+12.5k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+6k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+5.7k Golang : Find change in a combination of coins example
+8.8k Golang : automatically figure out array length(size) with three dots
+5.8k AWS S3 : Prevent Hotlinking policy
+6.7k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+18.8k Golang : Padding data for encryption and un-padding data for decryption
+47.8k Golang : How to convert JSON string to map and slice
+6.5k Golang : Check if password length meet the requirement