Created
November 6, 2014 13:58
-
-
Save tobz/56f50ebc0348bd2c0bb9 to your computer and use it in GitHub Desktop.
Benchmarking path.Match vs regular expressions for simple wildcard patterns
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 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