-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_speed_test.go
77 lines (69 loc) · 1.79 KB
/
string_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
// This file measures string performance as a baseline.
package intern_test
import (
"math/rand"
"testing"
)
// BenchmarkCompareRandomStrings compares a number of long, randomly generated
// strings.
func BenchmarkCompareRandomStrings(b *testing.B) {
// Create N strings with random contents.
if b.N < nComp {
return // Nothing to do
}
strs := generateRandomStrings(b.N)
// Measure the time needed to compare each string to each of the first
// nComp strings.
b.ResetTimer()
for _, s1 := range strs {
for _, s2 := range strs[:nComp] {
if s1 == s2 {
Dummy++
}
}
}
}
// BenchmarkCompareSimilarStrings compares a number of long strings that have a
// substantial prefix in common.
func BenchmarkCompareSimilarStrings(b *testing.B) {
// Create N mostly similar strings.
if b.N < nComp {
return // Nothing to do
}
strs := generateSimilarStrings(b.N)
// Measure the time needed to compare each string to each of the first
// nComp strings.
b.ResetTimer()
for _, s1 := range strs {
for _, s2 := range strs[:nComp] {
if s1 == s2 {
Dummy++
}
}
}
}
// BenchmarkMergeStringMaps measures the performance of retrieving a number of
// strings from a map.
func BenchmarkMergeStringMaps(b *testing.B) {
// Populate two maps.
prng := rand.New(rand.NewSource(2223)) // Constant for reproducibility
const sLen = 30 // Symbol length in characters
type Empty struct{}
m1 := make(map[string]Empty, b.N)
m2 := make(map[string]Empty, b.N)
for i := 0; i < b.N; i++ {
s := randomString(prng, sLen)
m1[s] = Empty{}
s = randomString(prng, sLen)
m2[s] = Empty{}
}
// Start the clock then merge the two maps into a third.
m3 := make(map[string]Empty, 2*b.N)
b.ResetTimer()
for k := range m1 {
m3[k] = Empty{}
}
for k := range m2 {
m3[k] = Empty{}
}
}