-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint_map_test.go
57 lines (47 loc) · 989 Bytes
/
bigint_map_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
package kademlia
import (
"math/big"
"math/rand"
"testing"
)
func newRand() *rand.Rand {
return rand.New(rand.NewSource(0))
}
func randBytes(rand *rand.Rand, size int) []byte {
b := make([]byte, size)
for i := range b {
b[i] = byte(rand.Intn(0xff))
}
return b
}
func randBigInts(n, size int) (ret []*big.Int) {
m, rand := make(map[string]struct{}), newRand()
for len(m) < n {
m[string(randBytes(rand, size))] = struct{}{}
}
for k := range m {
ret = append(ret, new(big.Int).SetBytes([]byte(k)))
}
return
}
func BenchmarkIntMapSet256(b *testing.B) {
keys := randBigInts(b.N, 256/8)
var m bigIntMap[struct{}]
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.set(keys[i], struct{}{})
}
}
func BenchmarkIntMapContains256(b *testing.B) {
keys := randBigInts(b.N, 256/8)
var m bigIntMap[struct{}]
for _, k := range keys {
m.set(k, struct{}{})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if !m.contains(keys[i]) {
panic("sould never happen")
}
}
}