-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark_test.go
90 lines (76 loc) · 1.83 KB
/
benchmark_test.go
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
package bitset
import (
"math/rand"
"testing"
"time"
"encoding/binary"
)
const randomSize = 1024 * 8
var (
randomSet = make([]uint, randomSize)
randomSet64 = make([]uint64, randomSize)
)
func init() {
rand.Seed(time.Now().UnixNano())
for i := 0; i < randomSize; i++ {
randomSet[i] = uint(rand.Intn(randomSize))
randomSet64[i] = uint64(rand.Intn(randomSize))
}
}
func BenchmarkBitVec_Set_LittleEndian(b *testing.B) {
benchmarkBitVecSet(b, binary.LittleEndian)
}
func BenchmarkBitVec_Set_BigEndian(b *testing.B) {
benchmarkBitVecSet(b, binary.BigEndian)
}
func benchmarkBitVecSet(b *testing.B, order binary.ByteOrder) {
buf := make([]byte, randomSize/8)
bv, err := New(buf, order, false)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
bv.Set(randomSet[i%randomSize])
}
}
func BenchmarkBitVec_Unset_LittleEndian(b *testing.B) {
benchmarkBitVecUnset(b, binary.LittleEndian)
}
func BenchmarkBitVec_Unset_BigEndian(b *testing.B) {
benchmarkBitVecUnset(b, binary.BigEndian)
}
func benchmarkBitVecUnset(b *testing.B, order binary.ByteOrder) {
buf := make([]byte, randomSize/8)
bv, err := New(buf, order, false)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
bv.Unset(randomSet[i%randomSize])
}
}
func BenchmarkBitVec_Get_LittleEndian(b *testing.B) {
benchmarkBitVecGet(b, binary.LittleEndian)
}
func BenchmarkBitVec_Get_BigEndian(b *testing.B) {
benchmarkBitVecGet(b, binary.BigEndian)
}
func benchmarkBitVecGet(b *testing.B, order binary.ByteOrder) {
buf := make([]byte, randomSize/8)
bv, err := New(buf, order, false)
if err != nil {
b.Fatal(err)
}
for _, v := range randomSet[:randomSize/2] {
bv.Set(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
bv.Get(randomSet[i%randomSize])
}
}
func BenchmarkSwap(b *testing.B) {
for i := 0; i < b.N; i++ {
swapUint64(randomSet64[i%randomSize])
}
}