Skip to content

Instantly share code, notes, and snippets.

@fiorix
Created April 18, 2014 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiorix/11047922 to your computer and use it in GitHub Desktop.
Save fiorix/11047922 to your computer and use it in GitHub Desktop.
Go's encoding/binary performance
package main
import (
"bytes"
"encoding/binary"
"math"
"testing"
)
func BenchmarkEncodeFloat1(b *testing.B) {
var buf bytes.Buffer
f := float32(math.MaxFloat32)
for n := 0; n < b.N; n++ {
binary.Write(&buf, binary.BigEndian, f)
}
// Use buf.Bytes()
}
func BenchmarkEncodeFloat2(b *testing.B) {
buf := make([]byte, 4)
f := float32(math.MaxFloat32)
for n := 0; n < b.N; n++ {
binary.BigEndian.PutUint32(buf, math.Float32bits(f))
}
// Use buf directly
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment