-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbench_test.go
126 lines (115 loc) · 2.58 KB
/
bench_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package sre2
// Note: This file is pulled from Go's regexp package, with non-benchmark tests removed. This is
// useful for comparison with the regexp baseline. Interestingly enough, because these tests all
// work on very 'small' strings, we're actually slower in most cases.
// TODO: Add a string prefix matcher (i.e. not as part of the regexp prog), as this seems to be
// where regexp gets most of its speed gains.
import (
"strings"
"testing"
)
func BenchmarkLiteral(b *testing.B) {
x := strings.Repeat("x", 50) + "y"
b.StopTimer()
re := MustParse("y")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.Match(x) {
println("no match!")
break
}
}
}
func BenchmarkNotLiteral(b *testing.B) {
x := strings.Repeat("x", 50) + "y"
b.StopTimer()
re := MustParse(".y")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.Match(x) {
println("no match!")
break
}
}
}
func BenchmarkMatchClass(b *testing.B) {
b.StopTimer()
x := strings.Repeat("xxxx", 20) + "w"
re := MustParse("[abcdw]")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.Match(x) {
println("no match!")
break
}
}
}
func BenchmarkMatchClass_InRange(b *testing.B) {
b.StopTimer()
// 'b' is between 'a' and 'c', so the charclass
// range checking is no help here.
x := strings.Repeat("bbbb", 20) + "c"
re := MustParse("[ac]")
b.StartTimer()
for i := 0; i < b.N; i++ {
if !re.Match(x) {
println("no match!")
break
}
}
}
/*
func BenchmarkReplaceAll(b *testing.B) {
x := "abcdefghijklmnopqrstuvwxyz"
b.StopTimer()
re := MustParse("[cjrw]")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.ReplaceAllString(x, "")
}
}
*/
func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
b.StopTimer()
x := "abcdefghijklmnopqrstuvwxyz"
re := MustParse("^zbc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}
func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
b.StopTimer()
x := strings.Repeat("abcdefghijklmnopqrstuvwxyz", 16)
re := MustParse("^zbc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}
func BenchmarkAnchoredShortMatch(b *testing.B) {
b.StopTimer()
x := "abcdefghijklmnopqrstuvwxyz"
re := MustParse("^.bc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}
func BenchmarkAnchoredLongMatch(b *testing.B) {
b.StopTimer()
x := strings.Repeat("abcdefghijklmnopqrstuvwxyz", 16)
re := MustParse("^.bc(d|e)")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match(x)
}
}
func BenchmarkComplexRe(b *testing.B) {
b.StopTimer()
re := MustParse(".*(a|(b))+(#*).+")
b.StartTimer()
for i := 0; i < b.N; i++ {
re.Match("aba#hello")
}
}