Skip to content

Instantly share code, notes, and snippets.

@bootjp
Last active March 22, 2019 23:41
Show Gist options
  • Select an option

  • Save bootjp/5b05b2082eba61c1c534aef9555dd7ec to your computer and use it in GitHub Desktop.

Select an option

Save bootjp/5b05b2082eba61c1c534aef9555dd7ec to your computer and use it in GitHub Desktop.
golang uuid string vs binary benchmark
package main
import (
"log"
"testing"
"github.com/google/uuid"
)
func uuidString() string {
uid, err := uuid.NewUUID()
if err != nil {
log.Println(err)
}
return uid.String()
}
func uuidBinary() []byte {
uid, err := uuid.NewUUID()
if err != nil {
log.Println(err)
}
b, err := uid.MarshalBinary()
if err != nil {
log.Fatal(err)
}
return b
}
func BenchmarkUUID_String(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
uuidString()
}
}
func BenchmarkUUID_Binary(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
uuidBinary()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment