Skip to content

Commit

Permalink
Testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Aug 17, 2018
1 parent 4ec4abb commit 87ba24d
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 7 deletions.
47 changes: 46 additions & 1 deletion bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,50 @@ func (b *BitSet) NextSet(i uint) (uint, bool) {
// }
//
func (b *BitSet) NextSetMany(i uint, buffer []uint) (uint, []uint) {
myanswer := buffer
capacity := cap(buffer)
x := int(i >> log2WordSize)
if x >= len(b.set) || capacity == 0 {
return 0, myanswer[:0]
}
word := b.set[x]
skip := i & (wordSize - 1)
word = (word >> skip) << skip
myanswer = myanswer[:capacity]
size := int(0)
for word != 0 {
r := trailingZeroes64(word)

t := word & ((^word) + 1)
myanswer[size] = r + uint(x<<6)
size++
if size == capacity {
goto End
}
word = word ^ t
}
for idx, word := range b.set[x+1:] {
//base += 64
for word != 0 {
r := trailingZeroes64(word)
t := word & ((^word) + 1)
myanswer[size] = r + uint(x + idx + 1) * 64 //base
size++
if size == capacity {
goto End
}
word = word ^ t
}
}
End:
if size > 0 {
return myanswer[size-1], myanswer[:size]
} else {
return 0, myanswer[:0]
}
}

func (b *BitSet) NextSetManyold(i uint, buffer []uint) (uint, []uint) {
myanswer := buffer[:0]
x := int(i >> log2WordSize)
if x >= len(b.set) {
Expand All @@ -269,8 +313,9 @@ func (b *BitSet) NextSetMany(i uint, buffer []uint) (uint, []uint) {
capacity := cap(buffer)
for len(myanswer) < capacity {
for w != 0 {
t := w & ((^w) + 1)
r := trailingZeroes64(w)

t := w & ((^w) + 1)
myanswer = append(myanswer, r+base)
if len(myanswer) == capacity {
goto End
Expand Down
198 changes: 192 additions & 6 deletions bitset_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ func BenchmarkLemireIterateManyb(b *testing.B) {
for v := uint(0); v <= 100000000; v += 100 {
bitmap.Set(v)
}
buffer := make([]uint, 256)
b.ResetTimer()
sum := uint(0)
buffer := make([]uint, 256)
for i := 0; i < b.N; i++ {
j := uint(0)
j, buffer = bitmap.NextSetMany(j, buffer)
Expand Down Expand Up @@ -196,10 +196,9 @@ func BenchmarkFlorianUekermannIterateMany(b *testing.B) {
var input = make([]uint64, 68)
setRnd(input, 4)
var bitmap = From(input)
buffer := make([]uint, 256)
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 {
Expand All @@ -212,13 +211,33 @@ func BenchmarkFlorianUekermannIterateMany(b *testing.B) {
return
}
}
// go test -bench=BenchmarkFlorianUekermannIterateMany
func BenchmarkFlorianUekermannIterateManyold(b *testing.B) {
var input = make([]uint64, 68)
setRnd(input, 4)
var bitmap = From(input)
buffer := make([]uint, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var last, batch = bitmap.NextSetManyold(0, buffer)
for len(batch) > 0 {
for _, idx := range batch {
checksum += idx
}
last, batch = bitmap.NextSetManyold(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
Expand All @@ -229,13 +248,13 @@ func BenchmarkFlorianUekermannIterateManyReg(b *testing.B) {
}
}

// function provided by FlorianUekermann
func good(set []uint64) (checksum uint) {
for wordIdx, word := range set {
var wordIdx = uint(wordIdx * 64)
for word != 0 {
var bitIdx = uint(trailingZeroes64(word))
word ^= 1 << bitIdx
// Do something with the result of the next line
var index = wordIdx + bitIdx
checksum += index
}
Expand All @@ -247,11 +266,178 @@ func BenchmarkFlorianUekermannIterateManyComp(b *testing.B) {
var input = make([]uint64, 68)
setRnd(input, 4)
b.ResetTimer()
var checksum = uint(0)
for i := 0; i < b.N; i++ {
checksum += good(input)
}
if checksum == 0 { // added just to fool ineffassign
return
}
}

// go test -bench=BenchmarkFlorianUekermannLowDensityIterateMany
func BenchmarkFlorianUekermannLowDensityIterateMany(b *testing.B) {
var input = make([]uint64, 1000000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 50000; i++ {
input[rnd.Uint64()%1000000] = 1
}
var bitmap = From(input)
buffer := make([]uint, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
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
}
}

// go test -bench=BenchmarkFlorianUekermannLowDensityIterateMany
func BenchmarkFlorianUekermannLowDensityIterateManyold(b *testing.B) {
var input = make([]uint64, 1000000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 50000; i++ {
input[rnd.Uint64()%1000000] = 1
}
var bitmap = From(input)
buffer := make([]uint, 256)

b.ResetTimer()
for i := 0; i < b.N; i++ {
var last, batch = bitmap.NextSetManyold(0, buffer)
for len(batch) > 0 {
for _, idx := range batch {
checksum += idx
}
last, batch = bitmap.NextSetManyold(last+1, batch)
}
}
if checksum == 0 { // added just to fool ineffassign
return
}
}

func BenchmarkFlorianUekermannLowDensityIterateManyReg(b *testing.B) {
var input = make([]uint64, 1000000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 50000; i++ {
input[rnd.Uint64()%1000000] = 1
}
var bitmap = From(input)
b.ResetTimer()
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
}
}

func BenchmarkFlorianUekermannLowDensityIterateManyComp(b *testing.B) {
var input = make([]uint64, 1000000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 50000; i++ {
input[rnd.Uint64()%1000000] = 1
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
checksum += good(input)
}
if checksum == 0 { // added just to fool ineffassign
return
}
}



// go test -bench=BenchmarkFlorianUekermannLowDensityIterateMany
func BenchmarkFlorianUekermannMidDensityIterateMany(b *testing.B) {
var input = make([]uint64, 10000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 30000; i++ {
input[rnd.Uint64()%10000] |= 1 << rnd.Uint64()
}
var bitmap = From(input)
buffer := make([]uint, 256)

b.ResetTimer()
for i := 0; i < b.N; i++ {
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
}
}

// go test -bench=BenchmarkFlorianUekermannLowDensityIterateMany
func BenchmarkFlorianUekermannMidDensityIterateManyold(b *testing.B) {
var input = make([]uint64, 10000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 30000; i++ {
input[rnd.Uint64()%10000] |= 1 << rnd.Uint64()
}
var bitmap = From(input)
buffer := make([]uint, 256)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var last, batch = bitmap.NextSetManyold(0, buffer)
for len(batch) > 0 {
for _, idx := range batch {
checksum += idx
}
last, batch = bitmap.NextSetManyold(last+1, batch)
}
}
if checksum == 0 { // added just to fool ineffassign
return
}
}

func BenchmarkFlorianUekermannMidDensityIterateManyReg(b *testing.B) {
var input = make([]uint64, 10000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 30000; i++ {
input[rnd.Uint64()%10000] |= 1 << rnd.Uint64()
}
var bitmap = From(input)
b.ResetTimer()
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
}
}

func BenchmarkFlorianUekermannMidDensityIterateManyComp(b *testing.B) {
var input = make([]uint64, 10000)
var rnd = rand.NewSource(0).(rand.Source64)
for i := 0; i < 30000; i++ {
input[rnd.Uint64()%10000] |= 1 << rnd.Uint64()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
checksum += good(input)
}
if checksum == 0 { // added just to fool ineffassign
return
}
}

var checksum = uint(0)

0 comments on commit 87ba24d

Please sign in to comment.