Skip to content

Instantly share code, notes, and snippets.

@tobz
Created November 6, 2014 13:58
Show Gist options
  • Save tobz/56f50ebc0348bd2c0bb9 to your computer and use it in GitHub Desktop.
Save tobz/56f50ebc0348bd2c0bb9 to your computer and use it in GitHub Desktop.
Benchmarking path.Match vs regular expressions for simple wildcard patterns
package matcher
import "path"
import "regexp"
import "testing"
func BenchmarkPathMatcher(b *testing.B) {
for i := 0; i < b.N; i++ {
matches, err := path.Match("web-*-001", "web-us-001")
if err != nil {
panic(err)
}
if !matches {
panic("path match failed to match")
}
}
}
func BenchmarkRegexpMatcher(b *testing.B) {
for i := 0; i < b.N; i++ {
matches, err := regexp.MatchString("web-.*-001", "web-us-001")
if err != nil {
panic(err)
}
if !matches {
panic("regexp match failed to match")
}
}
}
func BenchmarkCompilerRegexpMatcher(b *testing.B) {
r, err := regexp.Compile("web-.*-001")
if err != nil {
panic("couldn't compile regular expression")
}
for i := 0; i < b.N; i++ {
matches := r.MatchString("web-us-001")
if !matches {
panic("compiled regexp match failed to match")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment