有没有一种有效的方法来计算 golang 中的执行时间?
Is there an efficient way to calculate execution time in golang?
我正在寻找计算执行时间的最佳方法。
func main() {
start := time.Now()
time.Sleep(time.Second * 2)
//something doing here
elapsed := time.Since(start)
fmt.Printf("page took %s", elapsed)
}
上面的代码工作正常。
但是当我使用模板时,我必须为每个模板函数重新编写。
是否有高效计算执行时间的方法,包括模板?
如果您正在为整个函数计时,那么您可以使用 defer
来消除一些重复代码。
func elapsed(what string) func() {
start := time.Now()
return func() {
fmt.Printf("%s took %v\n", what, time.Since(start))
}
}
func main() {
defer elapsed("page")() // <-- The trailing () is the deferred call
time.Sleep(time.Second * 2)
}
specification says this about deferred calls:
Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked. Instead, deferred functions are invoked immediately before the surrounding function returns,
函数值 elapsed("page")
在 defer 语句中计算。 elapsed
函数记录当前时间,returns一个匿名函数。返回的匿名函数在周围函数 returns 之前立即被调用。匿名函数计算并打印经过的时间。
Bayta Darell提供的方案很完美
此外,如果你不想显式传递函数名,你可以这样实现:
func SomeFunction(list *[]string) {
defer TimeTrack(time.Now())
// Do whatever you want.
}
func TimeTrack(start time.Time) {
elapsed := time.Since(start)
// Skip this function, and fetch the PC and file for its parent.
pc, _, _, _ := runtime.Caller(1)
// Retrieve a function object this functions parent.
funcObj := runtime.FuncForPC(pc)
// Regex to extract just the function name (and not the module path).
runtimeFunc := regexp.MustCompile(`^.*\.(.*)$`)
name := runtimeFunc.ReplaceAllString(funcObj.Name(), "")
log.Println(fmt.Sprintf("%s took %s", name, elapsed))
}
因此,您将得到:
SomeFunction took 15.483µs
更多信息,请参考这篇文章:Go Function Tracing
分享知识。 :)
使用初始化函数
package main
import (
"fmt"
"time"
)
var start time.Time
func init() {
start = time.Now()
}
func getChars(s string) {
for _, c := range s {
fmt.Printf("%c at time %v\n", c, time.Since(start))
time.Sleep(10 * time.Millisecond)
}
}
func main() {
fmt.Println("main execution started at time", time.Since(start))
getChars("Hello")
fmt.Println("\nmain execution stopped at time", time.Since(start))
}
在golang中计算执行时间的有效方法
您可以使用延迟函数
在您的控制台上轻松获得执行时间
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.
func main() {
now := time.Now()
defer func() {
fmt.Println(time.Now().Sub(now))
}()
// Here you can do whatever you want
}
或者您可以使用此代码
func main() {
now := time.Now()
defer func() {
fmt.Println(time.Since(now))
}()
// Here you can do whatever you want
}
查看 Playground 中的代码了解更多信息。我添加了一些功能来从错误中恢复,同时打印执行时间,即使在出现紧急错误的情况下也是如此。
我正在寻找计算执行时间的最佳方法。
func main() {
start := time.Now()
time.Sleep(time.Second * 2)
//something doing here
elapsed := time.Since(start)
fmt.Printf("page took %s", elapsed)
}
上面的代码工作正常。
但是当我使用模板时,我必须为每个模板函数重新编写。
是否有高效计算执行时间的方法,包括模板?
如果您正在为整个函数计时,那么您可以使用 defer
来消除一些重复代码。
func elapsed(what string) func() {
start := time.Now()
return func() {
fmt.Printf("%s took %v\n", what, time.Since(start))
}
}
func main() {
defer elapsed("page")() // <-- The trailing () is the deferred call
time.Sleep(time.Second * 2)
}
specification says this about deferred calls:
Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked. Instead, deferred functions are invoked immediately before the surrounding function returns,
函数值 elapsed("page")
在 defer 语句中计算。 elapsed
函数记录当前时间,returns一个匿名函数。返回的匿名函数在周围函数 returns 之前立即被调用。匿名函数计算并打印经过的时间。
Bayta Darell提供的方案很完美
此外,如果你不想显式传递函数名,你可以这样实现:
func SomeFunction(list *[]string) {
defer TimeTrack(time.Now())
// Do whatever you want.
}
func TimeTrack(start time.Time) {
elapsed := time.Since(start)
// Skip this function, and fetch the PC and file for its parent.
pc, _, _, _ := runtime.Caller(1)
// Retrieve a function object this functions parent.
funcObj := runtime.FuncForPC(pc)
// Regex to extract just the function name (and not the module path).
runtimeFunc := regexp.MustCompile(`^.*\.(.*)$`)
name := runtimeFunc.ReplaceAllString(funcObj.Name(), "")
log.Println(fmt.Sprintf("%s took %s", name, elapsed))
}
因此,您将得到:
SomeFunction took 15.483µs
更多信息,请参考这篇文章:Go Function Tracing
分享知识。 :)
使用初始化函数
package main
import (
"fmt"
"time"
)
var start time.Time
func init() {
start = time.Now()
}
func getChars(s string) {
for _, c := range s {
fmt.Printf("%c at time %v\n", c, time.Since(start))
time.Sleep(10 * time.Millisecond)
}
}
func main() {
fmt.Println("main execution started at time", time.Since(start))
getChars("Hello")
fmt.Println("\nmain execution stopped at time", time.Since(start))
}
在golang中计算执行时间的有效方法
您可以使用延迟函数
在您的控制台上轻松获得执行时间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.
func main() {
now := time.Now()
defer func() {
fmt.Println(time.Now().Sub(now))
}()
// Here you can do whatever you want
}
或者您可以使用此代码
func main() {
now := time.Now()
defer func() {
fmt.Println(time.Since(now))
}()
// Here you can do whatever you want
}
查看 Playground 中的代码了解更多信息。我添加了一些功能来从错误中恢复,同时打印执行时间,即使在出现紧急错误的情况下也是如此。