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
+18.2k Unmarshal/Load CSV record into struct in Go
+9.8k Golang : How to get quoted string into another string?
+4.3k MariaDB/MySQL : How to get version information
+11.5k Golang : Convert decimal number(integer) to IPv4 address
+7.7k How to show different content from website server when AdBlock is detected?
+10.6k Golang : Create S3 bucket with official aws-sdk-go package
+12.1k Golang : Remove or trim extra comma from CSV
+8.1k Android Studio : Import third-party library or package into Gradle Scripts
+8.2k Golang : Convert(cast) []byte to io.Reader type
+9.8k Golang : cannot assign type int to value (type uint8) in range error
+19.9k Nginx + FastCGI + Go Setup.
+7.6k Golang : Get all countries phone codes