Commit 3a239a6a authored by Robert Griesemer's avatar Robert Griesemer

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 7d5c003a).

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/37213Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent ddb15cea
......@@ -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<<UintSize - 1))
s += ReverseBytes(uint(i))
}
Unused = uint64(s)
}
func BenchmarkReverseBytes16(b *testing.B) {
var s uint16
for i := 0; i < b.N; i++ {
ReverseBytes16(deBruijn64 & (1<<16 - 1))
s += ReverseBytes16(uint16(i))
}
Unused = uint64(s)
}
func BenchmarkReverseBytes32(b *testing.B) {
var s uint32
for i := 0; i < b.N; i++ {
ReverseBytes32(deBruijn64 & (1<<32 - 1))
s += ReverseBytes32(uint32(i))
}
Unused = uint64(s)
}
func BenchmarkReverseBytes64(b *testing.B) {
var s uint64
for i := 0; i < b.N; i++ {
ReverseBytes64(deBruijn64 & (1<<64 - 1))
s += ReverseBytes64(uint64(i))
}
Unused = s
}
func TestLen(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment