-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmap_lookup_test.go
77 lines (64 loc) · 1.36 KB
/
map_lookup_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
package main
import (
"math/rand"
"testing"
)
const (
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
benchSetSize = 1024
)
var (
benchPlaceholder bool
)
func BenchmarkMapUint64(b *testing.B) {
set := make(map[uint64]struct{}, benchSetSize)
keys := make([]uint64, 0, benchSetSize)
for i := 0; i < benchSetSize; i++ {
key := rand.Uint64()
set[key] = struct{}{}
keys = append(keys, key)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, k := range keys {
_, benchPlaceholder = set[k]
}
}
}
func BenchmarkMapString1(b *testing.B) {
benchmarkString(b, 1)
}
func BenchmarkMapString10(b *testing.B) {
benchmarkString(b, 10)
}
func BenchmarkMapString100(b *testing.B) {
benchmarkString(b, 100)
}
func BenchmarkMapString1000(b *testing.B) {
benchmarkString(b, 1000)
}
func BenchmarkMapString10000(b *testing.B) {
benchmarkString(b, 10000)
}
func genString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func benchmarkString(b *testing.B, n int) {
set := make(map[string]struct{}, benchSetSize)
keys := make([]string, 0, benchSetSize)
for i := 0; i < benchSetSize; i++ {
key := genString(n)
set[key] = struct{}{}
keys = append(keys, key)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, k := range keys {
_, benchPlaceholder = set[k]
}
}
}