Open
Description
From at least Go 1,4 onward,
$ go version
go version devel +e03220a594 Sat Aug 25 02:39:49 2018 +0000 linux/amd64
$
If we run a tiny benchmark (count++) and use StopTimer() for initialization (count = 0) then the benchmark runs for a very long time, perhaps forever.
$ cat tiny_test.go
package main
import (
"testing"
)
var count int64
func BenchmarkTiny(b *testing.B) {
for n := 0; n < b.N; n++ {
b.StopTimer()
count = 0
b.StartTimer()
count++
}
}
$
$ go test tiny_test.go -bench=.
goos: linux
goarch: amd64
BenchmarkTiny-4 ^Csignal: interrupt
FAIL command-line-arguments 143.633s
$
If we comment out StopTimer() (//b.StopTimer()) then the benchmark quickly runs to completion.
$ cat tiny_test.go
package main
import (
"testing"
)
var count int64
func BenchmarkTiny(b *testing.B) {
for n := 0; n < b.N; n++ {
// b.StopTimer()
count = 0
b.StartTimer()
count++
}
}
$
$ go test tiny_test.go -bench=.
goos: linux
goarch: amd64
BenchmarkTiny-4 1000000000 2.15 ns/op
PASS
ok command-line-arguments 2.374s
$