Skip to content

Instantly share code, notes, and snippets.

@mattes
Last active October 10, 2021 09:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattes/69a4ab7027b9e8ee952b5843e7ca6955 to your computer and use it in GitHub Desktop.
Save mattes/69a4ab7027b9e8ee952b5843e7ca6955 to your computer and use it in GitHub Desktop.
package test
import (
"regexp"
guuid "github.com/google/uuid"
suuid "github.com/satori/go.uuid"
)
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
func Func1(u string) bool {
return re.MatchString(u)
}
func Func2(u string) bool {
_, err := guuid.Parse(u)
return err == nil
}
func Func3(u string) bool {
_, err := suuid.FromString(u)
return err == nil
}
package test
import (
"testing"
)
var u = "2f6f97f2-5f44-476d-bc0c-180b2eaa36ca"
func Test_Func1(t *testing.T) {
if Func1(u) != true {
t.Fatal()
}
}
func Test_Func2(t *testing.T) {
if Func2(u) != true {
t.Fatal()
}
}
func Test_Func3(t *testing.T) {
if Func3(u) != true {
t.Fatal()
}
}
func Benchmark_Func1(b *testing.B) {
for i := 0; i < b.N; i++ {
Func1(u)
}
}
func Benchmark_Func2(b *testing.B) {
for i := 0; i < b.N; i++ {
Func2(u)
}
}
func Benchmark_Func3(b *testing.B) {
for i := 0; i < b.N; i++ {
Func3(u)
}
}
goos: darwin
goarch: amd64
Benchmark_Func1-4 3000000 588 ns/op 0 B/op 0 allocs/op
Benchmark_Func2-4 50000000 32 ns/op 0 B/op 0 allocs/op
Benchmark_Func3-4 20000000 106 ns/op 48 B/op 1 allocs/op
PASS
ok _/private/tmp/t3 6.137s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment