Skip to content

Commit

Permalink
math/bits: added benchmarks for Leading/TrailingZeros
Browse files Browse the repository at this point in the history
BenchmarkLeadingZeros-8      	200000000	         8.80 ns/op
BenchmarkLeadingZeros8-8     	200000000	         8.21 ns/op
BenchmarkLeadingZeros16-8    	200000000	         7.49 ns/op
BenchmarkLeadingZeros32-8    	200000000	         7.80 ns/op
BenchmarkLeadingZeros64-8    	200000000	         8.67 ns/op

BenchmarkTrailingZeros-8     	1000000000	         2.05 ns/op
BenchmarkTrailingZeros8-8    	2000000000	         1.94 ns/op
BenchmarkTrailingZeros16-8   	2000000000	         1.94 ns/op
BenchmarkTrailingZeros32-8   	2000000000	         1.92 ns/op
BenchmarkTrailingZeros64-8   	2000000000	         2.03 ns/op

Change-Id: I45497bf2d6369ba6cfc88ded05aa735908af8908
Reviewed-on: https://go-review.googlesource.com/37220
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
griesemer committed Feb 17, 2017
1 parent 19028bd commit a4a3d63
Showing 1 changed file with 114 additions and 34 deletions.
148 changes: 114 additions & 34 deletions src/math/bits/bits_test.go
Expand Up @@ -80,6 +80,56 @@ func TestLeadingZeros(t *testing.T) {
}
}

// Exported (global) variable serving as input for some
// of the benchmarks to ensure side-effect free calls
// are not optimized away.
var Input uint64 = deBruijn64

// Exported (global) variable to store function results
// during benchmarking to ensure side-effect free calls
// are not optimized away.
var Output int

func BenchmarkLeadingZeros(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += LeadingZeros(uint(Input) >> (uint(i) % UintSize))
}
Output = s
}

func BenchmarkLeadingZeros8(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += LeadingZeros8(uint8(Input) >> (uint(i) % 8))
}
Output = s
}

func BenchmarkLeadingZeros16(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += LeadingZeros16(uint16(Input) >> (uint(i) % 16))
}
Output = s
}

func BenchmarkLeadingZeros32(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += LeadingZeros32(uint32(Input) >> (uint(i) % 32))
}
Output = s
}

func BenchmarkLeadingZeros64(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += LeadingZeros64(uint64(Input) >> (uint(i) % 64))
}
Output = s
}

func TestTrailingZeros(t *testing.T) {
for i := 0; i < 256; i++ {
ntz := tab[i].ntz
Expand Down Expand Up @@ -141,6 +191,46 @@ func TestTrailingZeros(t *testing.T) {
}
}

func BenchmarkTrailingZeros(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += TrailingZeros(uint(Input) << (uint(i) % UintSize))
}
Output = s
}

func BenchmarkTrailingZeros8(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += TrailingZeros8(uint8(Input) << (uint(i) % 8))
}
Output = s
}

func BenchmarkTrailingZeros16(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += TrailingZeros16(uint16(Input) << (uint(i) % 16))
}
Output = s
}

func BenchmarkTrailingZeros32(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += TrailingZeros32(uint32(Input) << (uint(i) % 32))
}
Output = s
}

func BenchmarkTrailingZeros64(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += TrailingZeros64(uint64(Input) << (uint(i) % 64))
}
Output = s
}

func TestOnesCount(t *testing.T) {
for i := 0; i < 256; i++ {
want := tab[i].pop
Expand Down Expand Up @@ -189,54 +279,44 @@ func TestOnesCount(t *testing.T) {
}
}

// Exported (global) variable to store function results
// during benchmarking to ensure side-effect free calls
// are not optimized away.
var Unused int

// Exported (global) variable serving as input for some
// of the benchmarks to ensure side-effect free calls
// are not optimized away.
var Input uint64 = deBruijn64

func BenchmarkOnesCount(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += OnesCount(uint(Input))
}
Unused = s
Output = s
}

func BenchmarkOnesCount8(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += OnesCount8(uint8(Input))
}
Unused = s
Output = s
}

func BenchmarkOnesCount16(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += OnesCount16(uint16(Input))
}
Unused = s
Output = s
}

func BenchmarkOnesCount32(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += OnesCount32(uint32(Input))
}
Unused = s
Output = s
}

func BenchmarkOnesCount64(b *testing.B) {
var s int
for i := 0; i < b.N; i++ {
s += OnesCount64(uint64(Input))
}
Unused = s
Output = s
}

func TestRotateLeft(t *testing.T) {
Expand Down Expand Up @@ -294,39 +374,39 @@ func BenchmarkRotateLeft(b *testing.B) {
for i := 0; i < b.N; i++ {
s += RotateLeft(uint(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateLeft8(b *testing.B) {
var s uint8
for i := 0; i < b.N; i++ {
s += RotateLeft8(uint8(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateLeft16(b *testing.B) {
var s uint16
for i := 0; i < b.N; i++ {
s += RotateLeft16(uint16(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateLeft32(b *testing.B) {
var s uint32
for i := 0; i < b.N; i++ {
s += RotateLeft32(uint32(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateLeft64(b *testing.B) {
var s uint64
for i := 0; i < b.N; i++ {
s += RotateLeft64(uint64(Input), i)
}
Unused = int(s)
Output = int(s)
}

func TestRotateRight(t *testing.T) {
Expand Down Expand Up @@ -384,39 +464,39 @@ func BenchmarkRotateRight(b *testing.B) {
for i := 0; i < b.N; i++ {
s += RotateRight(uint(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateRight8(b *testing.B) {
var s uint8
for i := 0; i < b.N; i++ {
s += RotateRight8(uint8(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateRight16(b *testing.B) {
var s uint16
for i := 0; i < b.N; i++ {
s += RotateRight16(uint16(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateRight32(b *testing.B) {
var s uint32
for i := 0; i < b.N; i++ {
s += RotateRight32(uint32(Input), i)
}
Unused = int(s)
Output = int(s)
}

func BenchmarkRotateRight64(b *testing.B) {
var s uint64
for i := 0; i < b.N; i++ {
s += RotateRight64(uint64(Input), i)
}
Unused = int(s)
Output = int(s)
}

func TestReverse(t *testing.T) {
Expand Down Expand Up @@ -502,39 +582,39 @@ func BenchmarkReverse(b *testing.B) {
for i := 0; i < b.N; i++ {
s += Reverse(uint(i))
}
Unused = int(s)
Output = int(s)
}

func BenchmarkReverse8(b *testing.B) {
var s uint8
for i := 0; i < b.N; i++ {
s += Reverse8(uint8(i))
}
Unused = int(s)
Output = int(s)
}

func BenchmarkReverse16(b *testing.B) {
var s uint16
for i := 0; i < b.N; i++ {
s += Reverse16(uint16(i))
}
Unused = int(s)
Output = int(s)
}

func BenchmarkReverse32(b *testing.B) {
var s uint32
for i := 0; i < b.N; i++ {
s += Reverse32(uint32(i))
}
Unused = int(s)
Output = int(s)
}

func BenchmarkReverse64(b *testing.B) {
var s uint64
for i := 0; i < b.N; i++ {
s += Reverse64(uint64(i))
}
Unused = int(s)
Output = int(s)
}

func TestReverseBytes(t *testing.T) {
Expand Down Expand Up @@ -598,31 +678,31 @@ func BenchmarkReverseBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
s += ReverseBytes(uint(i))
}
Unused = int(s)
Output = int(s)
}

func BenchmarkReverseBytes16(b *testing.B) {
var s uint16
for i := 0; i < b.N; i++ {
s += ReverseBytes16(uint16(i))
}
Unused = int(s)
Output = int(s)
}

func BenchmarkReverseBytes32(b *testing.B) {
var s uint32
for i := 0; i < b.N; i++ {
s += ReverseBytes32(uint32(i))
}
Unused = int(s)
Output = int(s)
}

func BenchmarkReverseBytes64(b *testing.B) {
var s uint64
for i := 0; i < b.N; i++ {
s += ReverseBytes64(uint64(i))
}
Unused = int(s)
Output = int(s)
}

func TestLen(t *testing.T) {
Expand Down

0 comments on commit a4a3d63

Please sign in to comment.