MCPcopy Create free account
hub / github.com/dlclark/regexp2 / BenchmarkStringProcessing

Function BenchmarkStringProcessing

string_processing_benchmark_test.go:112–186  ·  view source on GitHub ↗
(b *testing.B)

Source from the content-addressed store, hash-verified

110}
111
112func BenchmarkStringProcessing(b *testing.B) {
113 for _, tc := range []struct {
114 name, input string
115 options RegexOptions
116 }{
117 {"ASCII", "needle" + strings.Repeat("x", 64<<10), None},
118 {"Unicode", "needle" + strings.Repeat("界", 21845), None},
119 {"UnicodeRTL", strings.Repeat("界", 21845) + "needle", RightToLeft},
120 } {
121 b.Run("FindAll/"+tc.name, func(b *testing.B) {
122 re := MustCompile(`needle`, tc.options)
123 b.ReportAllocs()
124 b.ResetTimer()
125 for i := 0; i < b.N; i++ {
126 got, err := re.FindAllStringIndex(tc.input, 1)
127 if err != nil || len(got) != 1 || tc.input[got[0][0]:got[0][1]] != "needle" {
128 b.Fatal(got, err)
129 }
130 }
131 })
132 b.Run("FindMatch/"+tc.name, func(b *testing.B) {
133 re := MustCompile(`needle`, tc.options)
134 b.ReportAllocs()
135 b.ResetTimer()
136 for i := 0; i < b.N; i++ {
137 got, err := re.FindStringMatch(tc.input)
138 if err != nil || got == nil || got.String() != "needle" {
139 b.Fatal(got, err)
140 }
141 }
142 })
143 }
144 for _, tc := range []struct {
145 name, input string
146 options RegexOptions
147 }{
148 {"Miss", strings.Repeat("x", 64<<10), None},
149 {"LateHit", strings.Repeat("x", 64<<10) + "needle", None},
150 {"LateHitRTL", strings.Repeat("x", 64<<10) + "needle", RightToLeft},
151 {"ManyHitsRTL", strings.Repeat("xxneedlexx", 256), RightToLeft},
152 {"ShortHit", "xxneedlexx", None},
153 } {
154 b.Run("Replace/"+tc.name, func(b *testing.B) {
155 re := MustCompile(`needle`, tc.options)
156 want := strings.ReplaceAll(tc.input, "needle", "pin")
157 b.ReportAllocs()
158 b.ResetTimer()
159 for i := 0; i < b.N; i++ {
160 got, err := re.Replace(tc.input, "pin", -1, -1)
161 if err != nil || got != want {
162 b.Fatal(got, err)
163 }
164 }
165 })
166 }
167 for _, tc := range []struct {
168 name, pattern string
169 want int

Callers

nothing calls this directly

Calls 6

FindStringMatchMethod · 0.80
ReplaceMethod · 0.80
SplitMethod · 0.80
MustCompileFunction · 0.70
FindAllStringIndexMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected