Skip to content

Instantly share code, notes, and snippets.

@muhlemmer
Last active March 3, 2023 09:34
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 muhlemmer/8a51518bc0c8f55ce83bf1d7952728c4 to your computer and use it in GitHub Desktop.
Save muhlemmer/8a51518bc0c8f55ce83bf1d7952728c4 to your computer and use it in GitHub Desktop.
Benchmark glob.Glob vs path.Match
package main
import (
"path"
"testing"
"github.com/gobwas/glob"
)
var patterns = []string{
"https://example.com",
"https://*.example.com",
"https://example.com/*",
}
var targets = []string{
"https://home.example.com",
"https://example.com",
"https://example.com/",
"https://example.com/user",
}
func Benchmark_glob_Glob(b *testing.B) {
for _, pattern := range patterns {
b.Run(pattern, func(b *testing.B) {
for i := 0; i < b.N; i++ {
g, err := glob.Compile(pattern)
if err != nil {
b.Fatal(err)
}
for _, target := range targets {
g.Match(target)
}
}
})
}
}
func Benchmark_path_Match(b *testing.B) {
for _, pattern := range patterns {
b.Run(pattern, func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, target := range targets {
path.Match(pattern, target)
}
}
})
}
}
~ go test -benchmem -bench ./
goos: linux
goarch: amd64
pkg: local/playground
cpu: AMD Ryzen 9 5900HX with Radeon Graphics
Benchmark_glob_Glob/https://example.com-16 1000000 1825 ns/op 704 B/op 15 allocs/op
Benchmark_glob_Glob/https://*.example.com-16 355215 4125 ns/op 1272 B/op 36 allocs/op
Benchmark_glob_Glob/https://example.com/*-16 772862 3780 ns/op 952 B/op 23 allocs/op
Benchmark_path_Match/https://example.com-16 4199529 301.7 ns/op 0 B/op 0 allocs/op
Benchmark_path_Match/https://*.example.com-16 856137 1296 ns/op 0 B/op 0 allocs/op
Benchmark_path_Match/https://example.com/*-16 3680846 333.6 ns/op 0 B/op 0 allocs/op
PASS
ok local/playground 11.334s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment