Skip to content

Commit

Permalink
test: add benchmark to compare float array encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jan 25, 2023
1 parent 57ff135 commit 99a4bbc
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions stream/enc_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package stream_test

import (
"io"
"math/rand"
"testing"

"github.com/go-faster/jx"
"github.com/go-faster/jx/stream"
)

func encodeFloats(e *jx.Encoder, arr []float64) {
e.ArrStart()
for _, v := range arr {
e.Float64(v)
}
e.ArrEnd()
}

func encodeFloatsStream[W io.Writer](e *stream.Encoder[W], arr []float64) bool {
if e.ArrStart() {
return true
}
for _, v := range arr {
if e.Float64(v) {
return true
}
}
return e.ArrEnd()
}

func BenchmarkEncodeFloats(b *testing.B) {
const N = 100_000
arr := make([]float64, N)
for i := 0; i < N; i++ {
arr[i] = rand.NormFloat64()
}
size := func() int64 {
var enc jx.Encoder
encodeFloats(&enc, arr)
return int64(len(enc.Bytes()))
}()
b.Logf("Size: %d bytes", size)

b.Run("Buffered", func(b *testing.B) {
b.SetBytes(size)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
// Notice: no buffer reuse.
var enc jx.Encoder
encodeFloats(&enc, arr)
}
})
})
b.Run("Stream", func(b *testing.B) {
b.SetBytes(size)
b.RunParallel(func(pb *testing.PB) {
enc := stream.NewEncoder(io.Discard)
for pb.Next() {
enc.Reset(io.Discard)
encodeFloatsStream(enc, arr)
}
})
})
}

0 comments on commit 99a4bbc

Please sign in to comment.