Skip to content

Commit

Permalink
Answer to #65
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Aug 17, 2018
1 parent 5159389 commit 2d4e848
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,13 @@ func (b *BitSet) NextSet(i uint) (uint, bool) {
//
func (b *BitSet) NextSetMany(i uint, buffer []uint) (uint, []uint) {
myanswer := buffer[:0]

x := int(i >> log2WordSize)
if x >= len(b.set) {
return 0, myanswer
}
w := b.set[x]
w = w >> (i & (wordSize - 1))
skip := i & (wordSize - 1)
w = (w >> skip) << skip
base := uint(x << 6)
capacity := cap(buffer)
for len(myanswer) < capacity {
Expand Down
48 changes: 48 additions & 0 deletions bitset_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,51 @@ func BenchmarkLemireIterateManyb(b *testing.B) {
return
}
}

func setRnd(bits []uint64, halfings int) {
var rnd = rand.NewSource(0).(rand.Source64)
for i := range bits {
bits[i] = 0xFFFFFFFFFFFFFFFF
for j := 0; j < halfings; j++ {
bits[i] &= rnd.Uint64()
}
}
}

// go test -bench=BenchmarkFlorianUekermannIterateMany
func BenchmarkFlorianUekermannIterateMany(b *testing.B) {
var input = make([]uint64, 68)
setRnd(input, 4)
var bitmap = From(input)
b.ResetTimer()
var checksum = uint(0)
for i := 0; i < b.N; i++ {
buffer := make([]uint, 256)
var last, batch = bitmap.NextSetMany(0, buffer)
for len(batch) > 0 {
for _, idx := range batch {
checksum += idx
}
last, batch = bitmap.NextSetMany(last+1, batch)
}
}
if checksum == 0 { // added just to fool ineffassign
return
}
}

func BenchmarkFlorianUekermannIterateManyReg(b *testing.B) {
var input = make([]uint64, 68)
setRnd(input, 4)
var bitmap = From(input)
b.ResetTimer()
var checksum = uint(0)
for i := 0; i < b.N; i++ {
for j, e := bitmap.NextSet(0); e; j, e = bitmap.NextSet(j + 1) {
checksum += j
}
}
if checksum == 0 { // added just to fool ineffassign
return
}
}

0 comments on commit 2d4e848

Please sign in to comment.