-
Notifications
You must be signed in to change notification settings - Fork 1
/
lge_speed_test.go
106 lines (97 loc) · 2.28 KB
/
lge_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
// This file measures the performance of operations on the LGE type.
package intern_test
import (
"testing"
"github.com/spakin/intern"
)
// BenchmarkLGECreation measures the time needed to create a symbol.
func BenchmarkLGECreation(b *testing.B) {
intern.ForgetAllLGEs()
strs := generateRandomStrings(b.N)
syms := make([]intern.LGE, len(strs))
b.ResetTimer()
for _, s := range strs {
intern.PreLGE(s)
}
var err error
for i, s := range strs {
syms[i], err = intern.NewLGE(s)
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkMultiLGECreation measures the time needed to create multiple
// symbols at once.
func BenchmarkMultiLGECreation(b *testing.B) {
intern.ForgetAllLGEs()
strs := generateRandomStrings(b.N)
b.ResetTimer()
intern.PreLGEMulti(strs)
_, err := intern.NewLGEMulti(strs)
if err != nil {
b.Fatal(err)
}
}
// BenchmarkCompareRandomLGEs compares a number of long, randomly generated
// strings by first mapping them to LGEs.
func BenchmarkCompareRandomLGEs(b *testing.B) {
// Create N LGEs with random contents.
intern.ForgetAllLGEs()
if b.N < nComp {
return // Nothing to do
}
strs := generateRandomStrings(b.N)
syms := make([]intern.LGE, len(strs))
for _, s := range strs {
intern.PreLGE(s)
}
var err error
for i, s := range strs {
syms[i], err = intern.NewLGE(s)
if err != nil {
b.Fatal(err)
}
}
// 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++
}
}
}
}
// BenchmarkCompareSimilarLGEs compares a number of long strings that have a
// substantial prefix in common by first mapping them to LGEs.
func BenchmarkCompareSimilarLGEs(b *testing.B) {
// Create LGEs for N mostly similar strings.
intern.ForgetAllLGEs()
if b.N < nComp {
return // Nothing to do
}
strs := generateSimilarStrings(b.N)
syms := make([]intern.LGE, len(strs))
for _, s := range strs {
intern.PreLGE(s)
}
var err error
for i, s := range strs {
syms[i], err = intern.NewLGE(s)
if err != nil {
b.Fatal(err)
}
}
// Measure the time needed to compare each LGE to each of the first
// nComp LGEs.
b.ResetTimer()
for _, s1 := range syms {
for _, s2 := range syms[:nComp] {
if s1 < s2 {
Dummy++
}
}
}
}