Last active
March 22, 2019 23:41
-
-
Save bootjp/5b05b2082eba61c1c534aef9555dd7ec to your computer and use it in GitHub Desktop.
golang uuid string vs binary benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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