-
Notifications
You must be signed in to change notification settings - Fork 1
/
eq_speed_test.go
110 lines (99 loc) · 2.66 KB
/
eq_speed_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This file measures the performance of operations on the Eq type.
package intern_test
import (
"math/rand"
"testing"
"github.com/spakin/intern"
)
// BenchmarkEqCreation measures the time needed to create a symbol.
func BenchmarkEqCreation(b *testing.B) {
intern.ForgetAllEqs()
strs := generateRandomStrings(b.N)
syms := make([]intern.Eq, len(strs))
b.ResetTimer()
for i, s := range strs {
syms[i] = intern.NewEq(s)
}
}
// BenchmarkMultiEqCreation measures the time needed to create multiple symbols
// at once.
func BenchmarkMultiEqCreation(b *testing.B) {
intern.ForgetAllEqs()
strs := generateRandomStrings(b.N)
b.ResetTimer()
_ = intern.NewEqMulti(strs)
}
// BenchmarkCompareRandomEqs compares a number of long, randomly generated
// strings by first mapping them to Eqs.
func BenchmarkCompareRandomEqs(b *testing.B) {
// Create N Eqs with random contents.
intern.ForgetAllEqs()
if b.N < nComp {
return // Nothing to do
}
strs := generateRandomStrings(b.N)
syms := make([]intern.Eq, len(strs))
for i, s := range strs {
syms[i] = intern.NewEq(s)
}
// Measure the time needed to compare each string to each of the first
// nComp strings.
b.ResetTimer()
for _, s1 := range syms {
for _, s2 := range syms[:nComp] {
if s1 == s2 {
Dummy++
}
}
}
}
// BenchmarkCompareSimilarEqs compares a number of long strings that have a
// substantial prefix in common by first mapping them to Eqs.
func BenchmarkCompareSimilarEqs(b *testing.B) {
// Create Eqs for N mostly similar strings.
intern.ForgetAllEqs()
if b.N < nComp {
return // Nothing to do
}
strs := generateSimilarStrings(b.N)
syms := make([]intern.Eq, len(strs))
for i, s := range strs {
syms[i] = intern.NewEq(s)
}
// Measure the time needed to compare each Eq to each of the first
// nComp Eqs.
b.ResetTimer()
for _, s1 := range syms {
for _, s2 := range syms[:nComp] {
if s1 == s2 {
Dummy++
}
}
}
}
// BenchmarkMergeEqMaps measures the performance of retrieving a number of Eqs
// from a map.
func BenchmarkMergeEqMaps(b *testing.B) {
// Populate two maps.
intern.ForgetAllEqs()
prng := rand.New(rand.NewSource(2223)) // Constant for reproducibility
const sLen = 20 // Symbol length in characters
type Empty struct{}
m1 := make(map[intern.Eq]Empty, b.N)
m2 := make(map[intern.Eq]Empty, b.N)
for i := 0; i < b.N; i++ {
s := randomString(prng, sLen)
m1[intern.NewEq(s)] = Empty{}
s = randomString(prng, sLen)
m2[intern.NewEq(s)] = Empty{}
}
// Start the clock then merge the two maps into a third.
m3 := make(map[intern.Eq]Empty, 2*b.N)
b.ResetTimer()
for k := range m1 {
m3[k] = Empty{}
}
for k := range m2 {
m3[k] = Empty{}
}
}