Different methods to measure execution time in GO
Sometimes during software development, we must determine which specific section of code is taking too long to execute leading to high golang timing of execution. We have to measure running time of individual function or complete go code in order to accomplish that.
There are numerous ways to calculate the runtime of a piece of code; in this post, I'll introduce you to the two easiest and most popular methods. These methods include the usage of time.Since()
function and using defer
statement.
Term | Description |
Defer statement |
A defer statement defers the execution of a function until the surrounding function returns.
The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns. |
func Since(t Time) Duration |
Since returns the time elapsed since t. It is shorthand for time.Now().Sub(t) . |
func (t Time) Sub(u Time) Duration |
Sub returns the duration t-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute t-d for a duration d , use t.Add (-d ). |
Method 1: Using time.Time() and time.Since() functions
A simple way to check golang timing is to use time.Now()
and time.Since()
functions. You'll need to import the time
package to use this.
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
// sum of 10000 consecutive numbers
sum := 0
for i := 1; i < 10000; i++ {
sum += i
}
fmt.Println("sum:", sum)
// calculate to exe time
elapsed := time.Since(start)
fmt.Printf("Sum function took %s", elapsed)
}
The same ideal but different way to implement:
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
// sum of 10000 consecutive numbers
sum := 0
for i := 1; i < 100000; i++ {
sum += i
}
fmt.Println("sum:", sum)
// calculate to exe time
//using time.Sub() function
elapsed := time.Now().Sub(start)
fmt.Printf("Sum function took %s", elapsed)
}
Output:
$ go run main.go
sum: 49995000
Sum function took 503.5µs
Explanation:
In the above code,
Using time.Now()
function to provide the exact time at that moment, indicates the time the program starts
Run the code we want to measure time of execution.
To get the actual time taken by the code, pass the result that got from time.Now()
function to time.Since()
function and have result. (or we can use time.Sub()
function to calculate the elapsed time between the function start and stop)
Method 2: Using defer statement
You can easily get the execution time on your console using a defer function.
defer functions execute even if the code get an error so you always get the execution time. time package is used to get the time difference.
package main
import (
"fmt"
"time"
)
func main() {
defer exeTime("main")()
// sum of 10000 consecutive numbers
sum := 0
for i := 1; i < 10; i++ {
sum += i
// sleep 1s
time.Sleep(time.Second * 1)
}
fmt.Println("sum:", sum)
}
// func to calculate and print execution time
func exeTime(name string) func() {
start := time.Now()
return func() {
fmt.Printf("%s execution time: %v\n", name, time.Since(start))
}
}
Output:
$ go run main.go
sum: 45
main execution time: 9.0736607s
Explanation:
Go's built-in defer
statement defers execution of the specified function until the current function returns. In this example, we want to measure how long it takes to run main
function. We start the exeTime
function, then wait the main
function to return. After that, defer will return the function to calculate execution time and print it to the console.
Summary
These are two efficient way to calculate execution time in golang. The ideal behind is simple: based on the time the function starts and stops, do a subtraction to calculate the execution time. Go provide build-in time
package which includes some function such as time.Sleep(), time.Since(), time.Sub() and time.Now() to compute the elapsed time. With the help of defer
statement, we can easily implement a function that calculate the execution time for other functions.
References
https://pkg.go.dev/time#Since
https://pkg.go.dev/time#Time.Sub