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
+4.3k JavaScript/JQuery : Redirect page examples
+3.8k Facebook : How to place save to Facebook button on your website
+22.2k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+17.2k Golang : Generate thumbnails from images
+8.4k Golang : Convert(cast) string to int64
+5.8k Default cipher that OpenSSL used to encrypt a PEM file
+13.3k Golang : Parsing or breaking down URL
+6k Golang : Squaring elements in array
+6.5k Golang : Handling Yes No Quit query input
+3.7k MariaDB/MySQL : How to get version information
+14k Golang : Get digits from integer before and after given position example