Skip to content

Instantly share code, notes, and snippets.

@yoppi
Last active October 20, 2017 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoppi/325bf2f849f5fb25b127f84b402ab43f to your computer and use it in GitHub Desktop.
Save yoppi/325bf2f849f5fb25b127f84b402ab43f to your computer and use it in GitHub Desktop.
キャプチャするしない
package regexp
import (
"regexp"
"testing"
)
func BenchmarkRegexp1(b *testing.B) {
re := regexp.MustCompile(`(iTunes)/(\d+)\.(\d+)`)
for i := 0; i < b.N; i++ {
re.MatchString("iTunes/12.7 (Macintosh; OS X 10.11.6) AppleWebKit/601.7.8")
re.MatchString("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/533.3 (KHTML, like Gecko) cXensebot/1.1a; +http://www.cxense.com/bot.html/0.1 Safari/533.3")
}
}
func BenchmarkRegexp2(b *testing.B) {
re := regexp.MustCompile(`iTunes/\d+\.\d+`)
for i := 0; i < b.N; i++ {
re.MatchString("iTunes/12.7 (Macintosh; OS X 10.11.6) AppleWebKit/601.7.8")
re.MatchString("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/533.3 (KHTML, like Gecko) cXensebot/1.1a; +http://www.cxense.com/bot.html/0.1 Safari/533.3")
}
}
$ go test -bench=. -benchmem [~/src/github.com/yoppi/go/regexp]
goos: darwin
goarch: amd64
pkg: github.com/yoppi/go/regexp
BenchmarkRegexp1-4 3000000 774 ns/op 0 B/op 0 allocs/op
BenchmarkRegexp3-4 3000000 521 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/yoppi/go/regexp 4.922s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment