From: Robert Griesemer Date: Fri, 17 Feb 2017 20:37:38 +0000 (-0800) Subject: math/bits: fix benchmarks (make sure calls don't get optimized away) X-Git-Tag: go1.9beta1~1517 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3a239a6ae44163d43cde40d9b83dc1c5b7359cb2;p=gostls13.git math/bits: fix benchmarks (make sure calls don't get optimized away) Sum up function results and store them in an exported (global) variable. This prevents the compiler from optimizing away the otherwise side-effect free function calls. We now have more realistic set of benchmark numbers... Measured on 2.3 GHz Intel Core i7, running maxOS 10.12.3. Note: These measurements are based on the same "old" implementation as the prior measurements (commit 7d5c003). benchmark old ns/op new ns/op delta BenchmarkReverse-8 72.9 8.50 -88.34% BenchmarkReverse8-8 13.2 2.17 -83.56% BenchmarkReverse16-8 21.2 2.89 -86.37% BenchmarkReverse32-8 36.3 3.55 -90.22% BenchmarkReverse64-8 71.3 6.81 -90.45% BenchmarkReverseBytes-8 11.2 3.49 -68.84% BenchmarkReverseBytes16-8 6.24 0.93 -85.10% BenchmarkReverseBytes32-8 7.40 1.55 -79.05% BenchmarkReverseBytes64-8 10.5 2.47 -76.48% Reverse-8 72.9ns ± 0% 8.5ns ± 0% ~ (p=1.000 n=1+1) Reverse8-8 13.2ns ± 0% 2.2ns ± 0% ~ (p=1.000 n=1+1) Reverse16-8 21.2ns ± 0% 2.9ns ± 0% ~ (p=1.000 n=1+1) Reverse32-8 36.3ns ± 0% 3.5ns ± 0% ~ (p=1.000 n=1+1) Reverse64-8 71.3ns ± 0% 6.8ns ± 0% ~ (p=1.000 n=1+1) ReverseBytes-8 11.2ns ± 0% 3.5ns ± 0% ~ (p=1.000 n=1+1) ReverseBytes16-8 6.24ns ± 0% 0.93ns ± 0% ~ (p=1.000 n=1+1) ReverseBytes32-8 7.40ns ± 0% 1.55ns ± 0% ~ (p=1.000 n=1+1) ReverseBytes64-8 10.5ns ± 0% 2.5ns ± 0% ~ (p=1.000 n=1+1) Change-Id: I8aef1334b84f6cafd25edccad7e6868b37969efb Reviewed-on: https://go-review.googlesource.com/37213 Reviewed-by: Matthew Dempsky --- diff --git a/src/math/bits/bits_test.go b/src/math/bits/bits_test.go index e1c7201daa..9a8ae926d3 100644 --- a/src/math/bits/bits_test.go +++ b/src/math/bits/bits_test.go @@ -367,34 +367,49 @@ func testReverse(t *testing.T, x64, want64 uint64) { } } +// Exported (global) variable to store function results +// during benchmarking, to ensure side-effect free calls +// are not optimized away. +var Unused uint64 + func BenchmarkReverse(b *testing.B) { + var s uint for i := 0; i < b.N; i++ { - Reverse(uint(i)) + s += Reverse(uint(i)) } + Unused = uint64(s) } func BenchmarkReverse8(b *testing.B) { + var s uint8 for i := 0; i < b.N; i++ { - Reverse8(uint8(i)) + s += Reverse8(uint8(i)) } + Unused = uint64(s) } func BenchmarkReverse16(b *testing.B) { + var s uint16 for i := 0; i < b.N; i++ { - Reverse16(uint16(i)) + s += Reverse16(uint16(i)) } + Unused = uint64(s) } func BenchmarkReverse32(b *testing.B) { + var s uint32 for i := 0; i < b.N; i++ { - Reverse32(uint32(i)) + s += Reverse32(uint32(i)) } + Unused = uint64(s) } func BenchmarkReverse64(b *testing.B) { + var s uint64 for i := 0; i < b.N; i++ { - Reverse64(uint64(i)) + s += Reverse64(uint64(i)) } + Unused = s } func TestReverseBytes(t *testing.T) { @@ -454,27 +469,35 @@ func testReverseBytes(t *testing.T, x64, want64 uint64) { } func BenchmarkReverseBytes(b *testing.B) { + var s uint for i := 0; i < b.N; i++ { - ReverseBytes(deBruijn64 & (1<