Created
June 29, 2023 12:41
-
-
Save omohayui/3af060173ec7b0393c221a1fad6870fb to your computer and use it in GitHub Desktop.
strings.Contains vs regexp.MatchString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bench | |
import ( | |
"regexp" | |
"strings" | |
"testing" | |
) | |
var ( | |
inputTexts = []string{ | |
"カワ", | |
"カワウソ", | |
"otter", | |
"ot", | |
} | |
names = []string{"コツメカワウソ", | |
"オオカワウソ", | |
"ユーラシアカワウソ", | |
"ビロードカワウソ", | |
"ツメナシカワウソ", | |
"かわうそ", | |
"otter", | |
"sea otter", | |
"liver otter", | |
"ラッコ", | |
} | |
) | |
func BenchmarkStringsContains(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
for _, inputText := range inputTexts { | |
for _, name := range names { | |
strings.Contains(name, inputText) | |
// fmt.Printf("%s, %s: %v\n", name, inputText, strings.Contains(name, inputText)) | |
} | |
} | |
} | |
} | |
func BenchmarkRegexMatchString(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
for _, inputText := range inputTexts { | |
for _, name := range names { | |
regexp.MatchString(inputText, name) | |
// fmt.Printf("%s, %s: %v\n", name, inputText, res) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result