我真的很想知道花多少时间来做这个 Action 。有人有什么最好的主意吗?或任何图书馆。我用简单的方法做了,但它似乎是错误的方法。因为我确实有更多在 go
我有那个代码:
package main
import (
"fmt"
"time"
)
func main() {
var d int = 0
var beginTime = time.Now()
for i := 0; i < 100000; i++ {
for c := 0; c < 100; c++ {
d = 1
}
}
var endTime = time.Now()
fmt.Println(beginTime, endTime)
fmt.Println(time.Since(beginTime))
fmt.Println(d)
}
最佳答案
使用 built-in Benchmarks .
A sample benchmark function looks like this:
func BenchmarkHello(b *testing.B) { for i := 0; i < b.N; i++ { fmt.Sprintf("hello") } }
The benchmark function must run the target code b.N times. During benchark execution, b.N is adjusted until the benchmark function lasts long enough to be timed reliably. The output
BenchmarkHello 10000000 282 ns/op
means that the loop ran 10000000 times at a speed of 282 ns per loop.
编辑:如果您想在运行时而不是在测试中测量做某事的时间,我认为您的方式很好。
关于Golang 获得一个 Action 的处理时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30098196/