Home  >  Article  >  Backend Development  >  How to get function execution time in Go language

How to get function execution time in Go language

青灯夜游
青灯夜游Original
2023-01-17 10:23:063091browse

In the Go language, you can use the Since() function in the time package to obtain the function execution time. Set a start time before the function is executed, and obtain the time interval from the start time to the present at the end of the function execution. This time interval is the execution time of the function; and the function execution time can be calculated using the time.Since() function, The syntax "time.Since(t)" will return the time elapsed from t to now.

How to get function execution time in Go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Use time.Since to calculate the execution time

The length of the function’s running time is an important indicator to measure the performance of this function, especially in comparison In benchmark tests, the simplest way to get the running time of a function is to set a starting time before the function is executed, and get the time interval from the starting time to the present when the function ends. This time interval is the function's operation hours.

In Go language, we can use the Since() function in the time package to obtain the running time of the function. The introduction of the Since() function in the official Go language documentation is as follows.

func Since(t Time) Duration

Since() function returns the time elapsed from t to now, which is equivalent to time.Now().Sub(t).

Example 1: Use the Since() function to obtain the running time of the function

package main
import (
    "fmt"
    "time"
)
func test() {
    start := time.Now() // 获取当前时间
    sum := 0
    for i := 0; i < 100000000; i++ {
        sum++
    }
    elapsed := time.Since(start)
    fmt.Println("该函数执行完成耗时:", elapsed)
}
func main() {
    test()
}

The running results are as follows:

该函数执行完成耗时: 39.8933ms

We mentioned time above The function of .Now().Sub() is similar to the Since() function. If you want to use time.Now().Sub() to obtain the running time of the function, you only need to simply modify line 14 of our code above.

Example 2: Use time.Now().Sub() to get the running time of the function

package main
import (
    "fmt"
    "time"
)
func test() {
    start := time.Now() // 获取当前时间
    sum := 0
    for i := 0; i < 100000000; i++ {
        sum++
    }
    elapsed := time.Now().Sub(start)
    fmt.Println("该函数执行完成耗时:", elapsed)
}
func main() {
    test()
}

The running results are as follows:

该函数执行完成耗时: 36.8769ms

Due to the influence of the computer's CPU and some other factors, the results obtained each time when obtaining the function running time are slightly different, which is normal.

Extended knowledge: use time.Now().Sub() to calculate the time difference

We only need to replace time.Since() with time.Now().Sub() can be used, as follows:

    start := time.Now() // 获取当前时间
    sum := 0
    for i := 0; i < 100000000; i++ {
        sum++
    }
    elapsed := time.Now().Sub(start)
    fmt.Println(elapsed)

In fact, time.Since internally calls the Sub function. Let’s enter the time package to see. The annotation means that Since returns the time that has passed since t. , time.Since is the abbreviation of time.Now().Sub(t),

\src\time\time.go 923:6

// Since returns the time elapsed since t.
// It is shorthand for time.Now().Sub(t).
func Since(t Time) Duration {
    var now Time
    if t.wall&hasMonotonic != 0 {
        // Common case optimization: if t has monotonic time, then Sub will use only it.
        now = Time{hasMonotonic, runtimeNano() - startNano, nil}
    } else {
        now = Now()
    }
    return now.Sub(t)
}

when We can also use time.Now().Sub(start).Seconds() to get the number of seconds that have passed, Hours to get the number of hours that have passed, etc. The corresponding ones can also be abbreviated as time.Since(start).Seconds(), time. Since(start).Seconds() etc.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How to get function execution time in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn