From 59d89671b917396dc1ef28194bbb1ae2f3d1b2f4 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 17 Aug 2018 18:48:20 -0400 Subject: [PATCH] Cleaning new benchmarks. --- bitset.go | 5 +- bitset_benchmark_test.go | 113 +++++++++++++++++++-------------------- 2 files changed, 56 insertions(+), 62 deletions(-) diff --git a/bitset.go b/bitset.go index 6cc2870..338d9cc 100644 --- a/bitset.go +++ b/bitset.go @@ -280,11 +280,10 @@ func (b *BitSet) NextSetMany(i uint, buffer []uint) (uint, []uint) { 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 + myanswer[size] = r + uint(x+idx+1)*64 size++ if size == capacity { goto End @@ -300,7 +299,7 @@ End: } } -func (b *BitSet) NextSetManyold(i uint, buffer []uint) (uint, []uint) { +func (b *BitSet) NextSetManyoldold(i uint, buffer []uint) (uint, []uint) { myanswer := buffer[:0] x := int(i >> log2WordSize) if x >= len(b.set) { diff --git a/bitset_benchmark_test.go b/bitset_benchmark_test.go index 32a9841..788fe48 100644 --- a/bitset_benchmark_test.go +++ b/bitset_benchmark_test.go @@ -196,9 +196,10 @@ 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 { @@ -211,33 +212,13 @@ 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 @@ -266,6 +247,7 @@ 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) } @@ -284,6 +266,7 @@ func BenchmarkFlorianUekermannLowDensityIterateMany(b *testing.B) { var bitmap = From(input) buffer := make([]uint, 256) b.ResetTimer() + var checksum = uint(0) for i := 0; i < b.N; i++ { var last, batch = bitmap.NextSetMany(0, buffer) for len(batch) > 0 { @@ -298,31 +281,6 @@ func BenchmarkFlorianUekermannLowDensityIterateMany(b *testing.B) { } } -// 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) @@ -331,6 +289,7 @@ func BenchmarkFlorianUekermannLowDensityIterateManyReg(b *testing.B) { } 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 @@ -348,6 +307,7 @@ func BenchmarkFlorianUekermannLowDensityIterateManyComp(b *testing.B) { input[rnd.Uint64()%1000000] = 1 } b.ResetTimer() + var checksum = uint(0) for i := 0; i < b.N; i++ { checksum += good(input) } @@ -356,8 +316,6 @@ func BenchmarkFlorianUekermannLowDensityIterateManyComp(b *testing.B) { } } - - // go test -bench=BenchmarkFlorianUekermannLowDensityIterateMany func BenchmarkFlorianUekermannMidDensityIterateMany(b *testing.B) { var input = make([]uint64, 10000) @@ -369,6 +327,7 @@ func BenchmarkFlorianUekermannMidDensityIterateMany(b *testing.B) { buffer := make([]uint, 256) b.ResetTimer() + var checksum = uint(0) for i := 0; i < b.N; i++ { var last, batch = bitmap.NextSetMany(0, buffer) for len(batch) > 0 { @@ -383,23 +342,59 @@ func BenchmarkFlorianUekermannMidDensityIterateMany(b *testing.B) { } } -// go test -bench=BenchmarkFlorianUekermannLowDensityIterateMany -func BenchmarkFlorianUekermannMidDensityIterateManyold(b *testing.B) { +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() + 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 + } +} + +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() + var checksum = uint(0) + for i := 0; i < b.N; i++ { + checksum += good(input) + } + if checksum == 0 { // added just to fool ineffassign + return + } +} + +func BenchmarkFlorianUekermannMidStrongDensityIterateMany(b *testing.B) { + var input = make([]uint64, 10000) + var rnd = rand.NewSource(0).(rand.Source64) + for i := 0; i < 200000; i++ { + input[rnd.Uint64()%10000] |= 1 << rnd.Uint64() + } var bitmap = From(input) buffer := make([]uint, 256) + b.ResetTimer() + var checksum = uint(0) for i := 0; i < b.N; i++ { - var last, batch = bitmap.NextSetManyold(0, buffer) + var last, batch = bitmap.NextSetMany(0, buffer) for len(batch) > 0 { for _, idx := range batch { checksum += idx } - last, batch = bitmap.NextSetManyold(last+1, batch) + last, batch = bitmap.NextSetMany(last+1, batch) } } if checksum == 0 { // added just to fool ineffassign @@ -407,14 +402,15 @@ func BenchmarkFlorianUekermannMidDensityIterateManyold(b *testing.B) { } } -func BenchmarkFlorianUekermannMidDensityIterateManyReg(b *testing.B) { +func BenchmarkFlorianUekermannMidStrongDensityIterateManyReg(b *testing.B) { var input = make([]uint64, 10000) var rnd = rand.NewSource(0).(rand.Source64) - for i := 0; i < 30000; i++ { + for i := 0; i < 200000; i++ { input[rnd.Uint64()%10000] |= 1 << rnd.Uint64() } 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 @@ -425,13 +421,14 @@ func BenchmarkFlorianUekermannMidDensityIterateManyReg(b *testing.B) { } } -func BenchmarkFlorianUekermannMidDensityIterateManyComp(b *testing.B) { +func BenchmarkFlorianUekermannMidStrongDensityIterateManyComp(b *testing.B) { var input = make([]uint64, 10000) var rnd = rand.NewSource(0).(rand.Source64) - for i := 0; i < 30000; i++ { + for i := 0; i < 200000; i++ { input[rnd.Uint64()%10000] |= 1 << rnd.Uint64() } b.ResetTimer() + var checksum = uint(0) for i := 0; i < b.N; i++ { checksum += good(input) } @@ -439,5 +436,3 @@ func BenchmarkFlorianUekermannMidDensityIterateManyComp(b *testing.B) { return } } - -var checksum = uint(0)