You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On Go 1.6, this runs in 4.20ns/op, while on Go 1.7, this runs in 0.28ns/op. While this sounds wonderful, the problem is that the above benchmark is completely useless on Go 1.7.
The current compiler compiles the BenchmarkReverse64 as the following:
which completely eliminates all of the code worth benchmarking.
The optimizations that the current compiler does is reasonable, IMHO. So I tried adjusting my benchmark to the following:
func BenchmarkReverse64(b *testing.B) {
u := uint64(0x123456789abcdef)
var v uint64
for i := 0; i < b.N; i++ {
v += ReverseUint64(u)
}
_ = v
}
My goal was to try and use the result of ReverseUint64 to defeat the optimization. However, the optimizations still occurred and the benchmark measures pretty much nothing.
In order to finally get it to measure properly I did the following hack:
func BenchmarkReverse64(b *testing.B) {
u := uint64(0x123456789abcdef)
var v uint64
for i := 0; i < b.N; i++ {
v += ReverseUint64(u)
}
if &v == nil {
b.Log(v) // Ensure that the compiler thinks that v is being used.
}
}
However, this is clunky and not very intuitive.
I propose that the assignment of a variable to _ by itself indicates that the variable is being used, and this prevents whatever optimization is screwing with benchmarks.
We thought about using _ = ... for runtime.KeepAlive(...). It seemed too much magic. This is similar. Just use a package-level sink. See #15843, #15277, #13347.
Benchmarking is hard, but the answer is not to change the language to make benchmarking easier. That just leads you down the garden path of measuring code that does not correspond to any real code.
Using
Go1.7rc6
The new SSA compiler is too smart ;)
Consider the following benchmark:
On Go 1.6, this runs in 4.20ns/op, while on Go 1.7, this runs in 0.28ns/op. While this sounds wonderful, the problem is that the above benchmark is completely useless on Go 1.7.
The current compiler compiles the
BenchmarkReverse64
as the following:which completely eliminates all of the code worth benchmarking.
The optimizations that the current compiler does is reasonable, IMHO. So I tried adjusting my benchmark to the following:
My goal was to try and use the result of
ReverseUint64
to defeat the optimization. However, the optimizations still occurred and the benchmark measures pretty much nothing.In order to finally get it to measure properly I did the following hack:
However, this is clunky and not very intuitive.
I propose that the assignment of a variable to
_
by itself indicates that the variable is being used, and this prevents whatever optimization is screwing with benchmarks./CC @randall77 @dr2chase @cherrymui
The text was updated successfully, but these errors were encountered: