Skip to content

Instantly share code, notes, and snippets.

@gholt
Created August 2, 2017 01:50
Show Gist options
  • Save gholt/fb0201cc8a4e0838f0a81e23b6fcd083 to your computer and use it in GitHub Desktop.
Save gholt/fb0201cc8a4e0838f0a81e23b6fcd083 to your computer and use it in GitHub Desktop.
package main
// Seconds Bytes
// Source 0.62s 50,331,648
// JSON 1.40s 146,725,206
// XML 9.14s 549,378,383
// GOB 0.33s 75,349,526
// Custom 0.17s 50,331,658
import (
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"encoding/xml"
"fmt"
"math/rand"
"time"
"github.com/gholt/brimtext"
)
func main() {
data := [][]string{{"", "Seconds", "Bytes"}}
start := time.Now()
src := make([][]uint16, 3)
for replica := range src {
src[replica] = make([]uint16, 8388608)
for partition := range src[replica] {
src[replica][partition] = uint16(rand.Int31() & 0xffff)
}
}
data = append(data, []string{"Source", fmt.Sprintf("%.02fs", float64(time.Now().Sub(start))/float64(time.Second)), brimtext.ThousandsSep(int64(len(src)*len(src[0])*2), ",")})
start = time.Now()
dst, err := json.Marshal(src)
if err != nil {
panic(err)
}
data = append(data, []string{"JSON", fmt.Sprintf("%.02fs", float64(time.Now().Sub(start))/float64(time.Second)), brimtext.ThousandsSep(int64(len(dst)), ",")})
dst = nil
start = time.Now()
dst, err = xml.Marshal(src)
if err != nil {
panic(err)
}
data = append(data, []string{"XML", fmt.Sprintf("%.02fs", float64(time.Now().Sub(start))/float64(time.Second)), brimtext.ThousandsSep(int64(len(dst)), ",")})
start = time.Now()
var gobBuf bytes.Buffer
err = gob.NewEncoder(&gobBuf).Encode(src)
if err != nil {
panic(err)
}
data = append(data, []string{"GOB", fmt.Sprintf("%.02fs", float64(time.Now().Sub(start))/float64(time.Second)), brimtext.ThousandsSep(int64(len(gobBuf.Bytes())), ",")})
dst = nil
start = time.Now()
var customBuf bytes.Buffer
err = binary.Write(&customBuf, binary.LittleEndian, byte(0)) // version
if err != nil {
panic(err)
}
err = binary.Write(&customBuf, binary.LittleEndian, byte(len(src))) // replicas
if err != nil {
panic(err)
}
err = binary.Write(&customBuf, binary.LittleEndian, uint64(len(src[0]))) // partitions
if err != nil {
panic(err)
}
for _, replica := range src {
err = binary.Write(&customBuf, binary.LittleEndian, replica)
if err != nil {
panic(err)
}
}
data = append(data, []string{"Custom", fmt.Sprintf("%.02fs", float64(time.Now().Sub(start))/float64(time.Second)), brimtext.ThousandsSep(int64(len(customBuf.Bytes())), ",")})
dst = nil
opts := brimtext.NewDefaultAlignOptions()
opts.Alignments = []brimtext.Alignment{brimtext.Left, brimtext.Right, brimtext.Right}
fmt.Println(brimtext.Align(data, opts))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment