Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
communitycreator
execution time

How to get execution time in golang

Gutha Vamsi Krishna

In this shot, we will learn how to calculate the execution time for a function in golang.

We will use the defer statement to execute a function right before the main() function returns.

Approach

We will follow the below approach:

  • We will create a function where at first we will capture the present time, and

  • We will return an anonymous function which will print the time difference,

  • Since we don’t want to print the time difference when it is called, so we will call this function with defer, then the anonymous function will execute at a later time, that is when the surrounding function returns.

  • Here we will use a surrounding function as main() function. It means the anonymous function will execute right before the main() function returns.

Let us take a look at an example.

Example

In the following example, we will create a function getExecutionDuration() to get execution duration. This function returns a function that prints the time difference, we will make it execute at a later period of time, using defer statement before getExecutionDuration() call.

The returned function will execute right before the main() function returns. So it will calculate how much duration the main() took to execute.

Code

package main

//import packages
import(
  "fmt"
  "time"
)

//program execution starts here
func main() {
    //deferred statement
    defer getExecutionDuration()()

    //make the program to sleep for 3 seconds
    time.Sleep(time.Second * 3)
}

//function to calculate time difference
func getExecutionDuration() func() {
    //get time when execution started
    start := time.Now()

    return func() {
        //executes right before main function returns
        fmt.Println(time.Since(start))
    }
}

Explanation

In the above code snippet,

  • In Line 5: We are importing the format package fmt, which is used to format the input and output.
  • In Line 6: We are importing the time package time, which contains functions like Now(), sleep(), etc…
  • In Line 10: Program execution starts from the main() function in golang.
  • In Line 12: We are calling a function getExecutionDuration()() using the defer statement, the second () is used to call the returned function.
  • In Line 15: We are deliberately making the program sleep for 3 seconds to show you the clear difference in execution time.
  • Line 19: From this line, we defined the function definition of the getExecutionDuration().
    • In Line 21: We capture the time when the function getExecutionDuration() is executed.
    • In Line 23: We are returning a function that will print the time difference from when the program execution started till this statement executes.
    • In Line 25: We are printing the execution duration of the main() function.
  • Note that here Line 25 executes right before the main() function returns.

RELATED TAGS

golang
communitycreator
execution time
RELATED COURSES

View all Courses

Keep Exploring