The problem is that you are not using b.N
, which tells you how many times to run the benchmark. Also, if you only want to benchmark the ORing, you should probably just initialize the array once, or at least call b.ResetTimer()
so the initialization is not counted.
Here's what I ended up with, which gives the expected results:
package main
import (
"math/rand"
"testing"
)
const (
little = 5000000
big = 50000000
)
var a = make([]uint32, big)
func init() {
for i := 0; i < big; i++ {
a[i] = rand.Uint32()
}
}
func benchOR(b *testing.B, l int) {
var result uint32
for _, u := range a[:l] {
result |= u
}
}
func BenchmarkLittle(b *testing.B) {
for i := 0; i < b.N; i++ {
benchOR(b, little)
}
}
func BenchmarkBig(b *testing.B) {
for i := 0; i < b.N; i++ {
benchOR(b, big)
}
}
My results:
BenchmarkLittle 500 3222064 ns/op
BenchmarkBig 50 32268023 ns/op