英文:
How timing function execution works in go?
问题
我正在尝试计时执行一个函数,但它总是返回0ns
,这很难相信。我在中间添加了1ns的睡眠,但它会产生奇怪的结果(当取消注释时)。以下是代码:
func pow2(base, exponent int64) int64 {
var result int64 = 1
for ; exponent > 0; exponent >>= 1 {
result *= base
}
return result
}
func Test_pow2(*testing.T) {
var base, exponent, n, expected int64 = 2, 256, 0, 0
var start time.Time
var duration time.Duration
start = time.Now()
n = fastExponentiationAlgorithm(base, exponent)
//time.Sleep(1 * time.Nanosecond)
duration = time.Since(start)
expected = math.Pow(base, exponent)
if n != expected {
fmt.Errorf("expected %+v but got %+v", expected, n)
} else {
fmt.Printf("pow2(%+v, %+v) executed in %dns\n", base, exponent, duration / time.Nanosecond)
}
}
希望有人能指出我的错误,谢谢。
编辑:根据Adrian和hymns for disco的建议,生成了一个基准测试,结果如下:
func Benchmark_pow2(b *testing.B) {
var base, exponent, n, expected int64 = 2, 10, 0, 0
expected = math.Pow(base, exponent)
b.ResetTimer()
n = pow2(base, exponent)
b.StopTimer()
if n != expected {
b.Errorf("expected %d but got %d", expected, n)
}
}
执行后,生成了以下输出:
Benchmark_pow2
test1_test.go:44: expected 20 but got 16
--- FAIL: Benchmark_pow2
在这里发生了什么?为什么会以这种方式失败?
英文:
I'm trying to time the execution of a function but it always returns 0ns
, which is hard to believe. Added a 1ns sleep in between but it does strange things (when uncommented). Here is the code:
func pow2(base, exponent int64) int64 {
var result int64 = 1
for ; exponent > 0; exponent >>= 1 {
result *= base
}
return result
}
func Test_pow2(*testing.T) {
var base, exponent, n, expected int64 = 2, 256, 0, 0
var start time.Time
var duration time.Duration
start = time.Now()
n = fastExponentiationAlgorithm(base, exponent)
//time.Sleep(1 * time.Nanosecond)
duration = time.Since(start)
expected = math.Pow(base, exponent)
if n != expected {
fmt.Errorf("expected %+v but got %+v", expected, n)
} else {
fmt.Printf("pow2(%+v, %+v) executed in %dns\n", base, exponent, duration / time.Nanosecond)
}
}
Hopefully someone can point out my mistake,
regards.
Edit: A benchmark was generated, as suggested by Adrian and hymns for disco, which resulted as the following:
func Benchmark_pow2(b *testing.B) {
var base, exponent, n, expected int64 = 2, 10, 0, 0
expected = math.Pow(base, exponent)
b.ResetTimer()
n = pow2(base, exponent)
b.StopTimer()
if n != expected {
b.Errorf("expected %d but got %d", expected, n)
}
}
After executing it, this output is generated:
Benchmark_pow2
test1_test.go:44: expected 20 but got 16
--- FAIL: Benchmark_pow2
What is happening in here? why is it failing in this way?
答案1
得分: 3
时间包的有效精度并不是很高。即使值达到了“纳秒精度”,这并不意味着时间实际上每纳秒更新一次。根据我的测试,当前时间的分辨率实际上是100纳秒,并且大约每0.5毫秒更新一次。这很可能会因操作系统、硬件和其他因素而有所不同。
如果你想正确计时,可以阅读测试包中的基准测试。基准测试提供了更好的精度,因为它们在循环中多次运行你的代码,以获取平均执行时间。这样可以测量那些在没有聚合时太小的时间(就像你的情况)。
英文:
The effective precision of the time package is not that great. Even though the value goes to "nanosecond precision", that doesn't mean the time actually updates every nanosecond. From my own testing, the resolution of the current time is effectively 100 nanoseconds and only updates about every 0.5 milliseconds. This will most likely vary depending on your OS, hardware, and other factors.
If you want to time things the right way, read about benchmarks in the testing package. Benchmarks offer better precision because they run your code multiple times in a loop, to get the average execution time. This allows you to measure times which would be too small when not aggregated (like in your case).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论