Last active
January 12, 2018 03:22
-
-
Save sunhay/4671ddbff6ec9a1f911ae58bde7cf4a6 to your computer and use it in GitHub Desktop.
Benchmarking dgryski/go-bits versus math/bits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bitsbench | |
import ( | |
dbits "github.com/dgryski/go-bits" | |
"math/bits" | |
"testing" | |
) | |
// Modelled after https://github.com/golang/go/blob/master/src/math/bits/bits_test.go | |
// Exported (global) variable serving as input for some | |
// of the benchmarks to ensure side-effect free calls | |
// are not optimized away. | |
var Input uint64 = 0x0218a392cd3d5dbf // deBruijn64 | |
// Exported (global) variable to store function results | |
// during benchmarking to ensure side-effect free calls | |
// are not optimized away. | |
var Output int | |
func BenchmarkCtzFast(b *testing.B) { | |
var s int | |
for i := 0; i < b.N; i++ { | |
s += int(dbits.Ctz(uint64(Input) << (uint(i) % 64))) | |
} | |
Output = s | |
} | |
func BenchmarkGoMathBitsTrailing(b *testing.B) { | |
var s int | |
for i := 0; i < b.N; i++ { | |
s += bits.TrailingZeros64(uint64(Input) << (uint(i) % 64)) | |
} | |
Output = s | |
} | |
func BenchmarkClzFast(b *testing.B) { | |
var s int | |
for i := 0; i < b.N; i++ { | |
s += int(dbits.Clz(uint64(Input) >> (uint(i) % 64))) | |
} | |
Output = s | |
} | |
func BenchmarkGoMathBitsLeading(b *testing.B) { | |
var s int | |
for i := 0; i < b.N; i++ { | |
s += bits.LeadingZeros64(uint64(Input) >> (uint(i) % 64)) | |
} | |
Output = s | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sunny.klair:scratch$ go test -bench=. | |
goos: darwin | |
goarch: amd64 | |
pkg: github.com/dgryski/go-bits | |
BenchmarkCtzFast-4 1000000000 2.02 ns/op | |
BenchmarkGoMathBitsTrailing-4 2000000000 1.09 ns/op | |
BenchmarkClzFast-4 1000000000 2.17 ns/op | |
BenchmarkGoMathBitsLeading-4 2000000000 1.15 ns/op | |
PASS | |
ok workspace/scratch 7.347s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment