Created
July 23, 2015 14:51
-
-
Save yalue/3dcde97867f06cf0eec8 to your computer and use it in GitHub Desktop.
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 main | |
import ( | |
"fmt" | |
"math/rand" | |
"regexp" | |
"time" | |
) | |
func getRandomBytes(n uint32) []byte { | |
rand.Seed(0) | |
toReturn := make([]byte, n) | |
for i := 0; i < len(toReturn); i++ { | |
toReturn[i] = byte(rand.Int()) | |
} | |
return toReturn | |
} | |
var tests = []*regexp.Regexp{ | |
regexp.MustCompile(`abcdef ghijklmnopqrstuv`), | |
regexp.MustCompile(`abcdef.{0,20}ghijklmnopqrstuv`), | |
regexp.MustCompile(`(?i)abcdef.{0,20}ghijklmnopqrstuv`), | |
regexp.MustCompile(`(abc|def).{0,20}ghijklmnopqrstuv`), | |
} | |
func main() { | |
fmt.Printf("Generating random data... ") | |
data := getRandomBytes(15 * 1024 * 1024) | |
fmt.Printf("done.\n") | |
matched := false | |
var t time.Time | |
var elapsed time.Duration | |
for i, re := range tests { | |
t = time.Now() | |
matched = re.Match(data) | |
elapsed = time.Now().Sub(t) | |
fmt.Printf("Test %d took %f seconds.\n", i, elapsed.Seconds()) | |
if matched { | |
fmt.Printf("How did a match occur in random data?\n") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2017 update: case-insensitive regexes or regexes with
|
are still much slower:Go Version:
go version go1.8 windows/amd64