Created
June 29, 2023 13:00
-
-
Save omohayui/0e6d29bb62e2fca508fd91ad98a14ac6 to your computer and use it in GitHub Desktop.
strings.Contains vs regexp.MatchString (with ToLower and MustCompile)
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, strings.ToLower(inputText)) | |
// fmt.Printf("%s, %s: %v\n", name, inputText, strings.Contains(name, strings.ToLower(inputText))) | |
} | |
} | |
} | |
} | |
func BenchmarkRegexMatchString(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
for _, inputText := range inputTexts { | |
re := regexp.MustCompile(`(?i)` + inputText) | |
for _, name := range names { | |
re.MatchString(name) | |
// fmt.Printf("%s, %s: %v\n", name, inputText, re.MatchString(name)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result