Skip to content

Instantly share code, notes, and snippets.

@zserge
Created May 2, 2015 14:12
Show Gist options
  • Save zserge/2c5ffcff186fd5a1f4f1 to your computer and use it in GitHub Desktop.
Save zserge/2c5ffcff186fd5a1f4f1 to your computer and use it in GitHub Desktop.
Benchmarks for various encodings (Gob is the fastest, obviously, JSON is ~4 times slower, CSV is ~10 times slower)
package main
import (
"bytes"
"encoding/csv"
"encoding/gob"
"encoding/json"
"math/rand"
"testing"
)
func makeBlob() []byte {
blob := make([]byte, 1000000, 1000000)
for i := 0; i < len(blob); i++ {
blob[i] = byte(rand.Intn(0x100))
}
return blob
}
func BenchmarkJSONBinary(b *testing.B) {
blob := makeBlob()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(blob); err != nil {
b.Error(err)
}
}
}
func BenchmarkCSVBinary(b *testing.B) {
blob := makeBlob()
out := &bytes.Buffer{}
w := csv.NewWriter(out)
for i := 0; i < b.N; i++ {
if err := w.Write([]string{string(blob)}); err != nil {
b.Error(err)
}
}
}
func BenchmarkGobBinary(b *testing.B) {
blob := makeBlob()
out := &bytes.Buffer{}
w := gob.NewEncoder(out)
for i := 0; i < b.N; i++ {
if err := w.Encode(blob); err != nil {
b.Error(err)
}
}
}
func makeShortText() []byte {
text := make([]byte, 1000, 1000)
for i := 0; i < len(text); i++ {
text[i] = byte(rand.Intn(26) + 'a')
}
return text
}
func BenchmarkJSONShortText(b *testing.B) {
text := makeShortText()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(text); err != nil {
b.Error(err)
}
}
}
func BenchmarkCSVShortText(b *testing.B) {
text := makeShortText()
out := &bytes.Buffer{}
w := csv.NewWriter(out)
for i := 0; i < b.N; i++ {
if err := w.Write([]string{string(text)}); err != nil {
b.Error(err)
}
}
}
func BenchmarkGobShortText(b *testing.B) {
text := makeShortText()
out := &bytes.Buffer{}
w := gob.NewEncoder(out)
for i := 0; i < b.N; i++ {
if err := w.Encode(text); err != nil {
b.Error(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment