mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
add a match arena to regexp to avoid generating garbage.
simple regexps run 20x faster. the regex-dna benchmark goes 3x faster. R=rsc CC=golang-dev https://golang.org/cl/156108
This commit is contained in:
parent
b30f753dc3
commit
c62069cc1f
2 changed files with 134 additions and 47 deletions
|
|
@ -60,6 +60,7 @@ type tester struct {
|
|||
}
|
||||
|
||||
var matches = []tester{
|
||||
tester{`^abcdefg`, "abcdefg", vec{0, 7}},
|
||||
tester{`a+`, "baaab", vec{1, 4}},
|
||||
tester{"abcd..", "abcdef", vec{0, 6}},
|
||||
tester{``, "", vec{0, 0}},
|
||||
|
|
@ -450,3 +451,29 @@ func TestAllMatches(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLiteral(b *testing.B) {
|
||||
x := strings.Repeat("x", 50);
|
||||
b.StopTimer();
|
||||
re, _ := Compile(x);
|
||||
b.StartTimer();
|
||||
for i := 0; i < b.N; i++ {
|
||||
if !re.MatchString(x) {
|
||||
println("no match!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNotLiteral(b *testing.B) {
|
||||
x := strings.Repeat("x", 49);
|
||||
b.StopTimer();
|
||||
re, _ := Compile("^" + x);
|
||||
b.StartTimer();
|
||||
for i := 0; i < b.N; i++ {
|
||||
if !re.MatchString(x) {
|
||||
println("no match!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue