Skip to content

Instantly share code, notes, and snippets.

@pswaminathan
Last active August 27, 2019 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pswaminathan/6d78c99dc9768f09019c to your computer and use it in GitHub Desktop.
Save pswaminathan/6d78c99dc9768f09019c to your computer and use it in GitHub Desktop.
Benchmarking Go (Golang) UUID Packages
pswaminathan at home in ~/uuidbench
$ gb test -v -bench=. -test.benchmem
uuids
testing: warning: no tests to run
PASS
BenchmarkPbormanParse-4	 3000000	       599 ns/op	      80 B/op	       5 allocs/op
BenchmarkPbormanDump-4 	  200000	      6756 ns/op	    1520 B/op	      60 allocs/op
BenchmarkSatoriParse-4 	 1000000	      1149 ns/op	     240 B/op	       5 allocs/op
BenchmarkSatoriDump-4  	 3000000	       584 ns/op	     240 B/op	       5 allocs/op
pswaminathan at mm-mac-3325 in ~/uuidbench
$ gb test -v -bench=. -test.benchmem
uuids
testing: warning: no tests to run
PASS
BenchmarkPbormanParse-4	20000000	       116 ns/op	      16 B/op	       1 allocs/op
BenchmarkPbormanDump-4 	 1000000	      1347 ns/op	     304 B/op	      12 allocs/op
BenchmarkSatoriParse-4 	10000000	       219 ns/op	      48 B/op	       1 allocs/op
BenchmarkSatoriDump-4  	20000000	       112 ns/op	      48 B/op	       1 allocs/op
package uuids
import (
pb "github.com/pborman/uuid"
satori "github.com/satori/go.uuid"
"testing"
)
var (
testStrings = []string{
"7fb0533d-91a0-4d00-ad8e-37958e04acc2",
"3f5c5371-49f1-4500-a091-e472ac8a9ee2",
"e8365375-5608-4e00-9c83-68a11fab829e",
"640653a0-cad8-4200-b52c-8a59157874f7",
"640653a0-cad8-4200-b52c-8a59157874f7",
}
testPBs []pb.UUID
testSTs []satori.UUID
)
func init() {
testPBs = make([]pb.UUID, len(testStrings))
testSTs = make([]satori.UUID, len(testStrings))
for i, s := range testStrings {
testPBs[i] = pb.Parse(s)
testSTs[i] = satori.FromStringOrNil(s)
}
}
func BenchmarkPbormanParse(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, s := range testStrings {
uuid := pb.Parse(s)
_ = uuid
}
}
}
func BenchmarkPbormanDump(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, uuid := range testPBs {
s := uuid.String()
_ = s
}
}
}
func BenchmarkSatoriParse(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, s := range testStrings {
uuid := satori.FromStringOrNil(s)
_ = uuid
}
}
}
func BenchmarkSatoriDump(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, uuid := range testSTs {
s := uuid.String()
_ = s
}
}
}
package uuids
import (
pb "github.com/pborman/uuid"
satori "github.com/satori/go.uuid"
"testing"
)
var (
testString = "640653a0-cad8-4200-b52c-8a59157874f7"
testPB pb.UUID
testST satori.UUID
)
func init() {
testPB = pb.Parse(testString)
testST = satori.FromStringOrNil(testString)
}
func BenchmarkPbormanParse(b *testing.B) {
for i := 0; i < b.N; i++ {
uuid := pb.Parse(testString)
_ = uuid
}
}
func BenchmarkPbormanDump(b *testing.B) {
for i := 0; i < b.N; i++ {
s := testPB.String()
_ = s
}
}
func BenchmarkSatoriParse(b *testing.B) {
for i := 0; i < b.N; i++ {
uuid := satori.FromStringOrNil(testString)
_ = uuid
}
}
func BenchmarkSatoriDump(b *testing.B) {
for i := 0; i < b.N; i++ {
s := testST.String()
_ = s
}
}
@pswaminathan
Copy link
Author

If you're just doing parsing, https://github.com/pborman/uuid is faster. Overall, when including dumping back out to a string, https://github.com/satori/go.uuid ends up being a better choice, as pborman/uuid's UUID.String() method is horribly slow.

@egoldschmidt
Copy link

FYI since these benchmarks were done it looks like the pborman/uuid folks have checked in some performance improvements (pborman/uuid#13). My results on a 2014 Macbook Pro show similar dump performance and much better parse performance for pborman/uuid:

> $ go test -test.v -test.bench=. -test.benchmem
goos: darwin
goarch: amd64
pkg: github.com/egoldschmidt/go-uuid-bench
BenchmarkPbormanParse-8   	 5000000	       315 ns/op	      80 B/op	       5 allocs/op
BenchmarkPbormanDump-8    	 5000000	       362 ns/op	     240 B/op	       5 allocs/op
BenchmarkSatoriParse-8    	 2000000	       614 ns/op	     240 B/op	       5 allocs/op
BenchmarkSatoriDump-8     	 5000000	       333 ns/op	     240 B/op	       5 allocs/op
PASS

(Uses the multi test code from above)

@lggomez
Copy link

lggomez commented Aug 12, 2019

An update: here are the results of the benchmarks using the current and more maintained forks of both packages:

satori "github.com/gofrs/uuid"
pb "github.com/google/uuid"

Results on a 2017 Macbook Pro with go 1.12.6. I´m doubtful about PbormanParse not doing any allocs but the rest doesn't seem to be biased:

go test -test.v -test.bench=. -test.benchmem
goos: darwin
goarch: amd64
pkg: uuids/uuids
BenchmarkPbormanParse-4         50000000                34.5 ns/op             0 B/op          0 allocs/op
BenchmarkPbormanDump-4          20000000                66.3 ns/op            48 B/op          1 allocs/op
BenchmarkSatoriParse-4          20000000               121 ns/op              48 B/op          1 allocs/op
BenchmarkSatoriDump-4           20000000                62.2 ns/op            48 B/op          1 allocs/op
PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment