Repository navigation
Expand file tree
/
Copy pathbench64_test.go
More file actions
161 lines (152 loc) · 3.82 KB
/
Copy pathbench64_test.go
File metadata and controls
161 lines (152 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package gocroaring_test
import (
"math/rand"
"testing"
"github.com/RoaringBitmap/gocroaring"
)
// Benchmarks of the 64-bit bitmaps. The queries cycle through many probes, so
// that the branch predictor cannot learn the answers.
const probes64 = 4096
type bench64Data struct {
name string
bm *gocroaring.Bitmap64
vals []uint64 // the values of bm
}
// bench64Bitmaps returns bitmaps of 1M values: "sparse" spreads them over
// 2^48 (one value per 32-bit high part, so the ART is deep and wide),
// "clustered" puts them in 64 dense clusters over 2^40, and "dense" over
// 2^26.
func bench64Bitmaps() []bench64Data {
r := rand.New(rand.NewSource(64))
make64 := func(name string, gen func() uint64) bench64Data {
bm := gocroaring.New64()
for i := 0; i < 1000000; i++ {
bm.Add(gen())
}
return bench64Data{name, bm, bm.ToArray()}
}
return []bench64Data{
make64("sparse", func() uint64 { return uint64(r.Int63n(1 << 48)) }),
make64("clustered", func() uint64 {
return uint64(r.Intn(64))<<34 | uint64(r.Intn(1<<20))
}),
make64("dense", func() uint64 { return uint64(r.Int63n(1 << 26)) }),
}
}
// mixedProbes64 returns probes that are half present values and half random
// values in the range of the bitmap.
func mixedProbes64(d bench64Data) []uint64 {
r := rand.New(rand.NewSource(65))
max := d.vals[len(d.vals)-1]
out := make([]uint64, probes64)
for i := range out {
if i%2 == 0 {
out[i] = d.vals[r.Intn(len(d.vals))]
} else {
out[i] = uint64(r.Int63n(int64(max) + 1))
}
}
return out
}
func Benchmark64Queries(b *testing.B) {
var sink uint64
for _, d := range bench64Bitmaps() {
bm, probes := d.bm, mixedProbes64(d)
r := rand.New(rand.NewSource(66))
ranks := make([]uint64, probes64)
for i := range ranks {
ranks[i] = uint64(r.Intn(len(d.vals)))
}
const mask = probes64 - 1
b.Run("Contains/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
if bm.Contains(probes[n&mask]) {
sink++
}
}
})
b.Run("Rank/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
sink += bm.Rank(probes[n&mask])
}
})
b.Run("Select/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
v, _ := bm.Select(ranks[n&mask])
sink += v
}
})
b.Run("RemoveAdd/"+d.name, func(b *testing.B) {
// Remove a present value and put it back: the bitmap does not
// change, but both updates do their work.
for n := 0; n < b.N; n++ {
v := probes[(n&mask)&^1]
bm.Remove(v)
bm.Add(v)
}
})
b.Run("AddNew/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
if n%len(d.vals) == 0 {
b.StopTimer()
bm = gocroaring.New64()
b.StartTimer()
}
bm.Add(d.vals[n%len(d.vals)] ^ 0x5555)
}
bm = d.bm
})
b.Run("Iterate/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
it := bm.NewIterator()
for it.HasValue() {
sink += it.Value()
it.Next()
}
}
})
b.Run("Cardinality/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
sink += bm.Cardinality()
}
})
}
_ = sink
}
func Benchmark64SetOps(b *testing.B) {
bms := bench64Bitmaps()
r := rand.New(rand.NewSource(67))
var sink uint64
for _, d := range bms {
// A second bitmap that shares about half of the values of the first.
other := gocroaring.New64()
for _, v := range d.vals {
if r.Intn(2) == 0 {
other.Add(v)
} else {
other.Add(v ^ 1)
}
}
a := d.bm
b.Run("Or/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
x := gocroaring.Or64(a, other)
sink += x.Cardinality()
x.Free()
}
})
b.Run("And/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
x := gocroaring.And64(a, other)
sink += x.Cardinality()
x.Free()
}
})
b.Run("AndCardinality/"+d.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
sink += a.AndCardinality(other)
}
})
}
_ = sink
}