Skip to content

Instantly share code, notes, and snippets.

@pascaldekloe
Last active April 2, 2023 13:41
Show Gist options
  • Save pascaldekloe/9af7d2bb1a1bc88e162037bbc213f374 to your computer and use it in GitHub Desktop.
Save pascaldekloe/9af7d2bb1a1bc88e162037bbc213f374 to your computer and use it in GitHub Desktop.
Go serialization speed test
// Package wordbench measures serialization speed.
package wordbench
import (
"encoding/binary"
"fmt"
"math/rand"
"testing"
)
func randN64Bit(n int) []uint64 {
r := rand.New(rand.NewSource(rand.Int63()))
ints := make([]uint64, n)
for i := range ints {
ints[i] = r.Uint64()
}
return ints
}
var data = randN64Bit(1024 * 1024)
var buf8 = make([]uint8, len(data)*8)
var buf64 = make([]uint64, len(data))
// BenchmarkShuffle64Bits writes 10 words in arbitrary order.
func BenchmarkShuffle64Bit(b *testing.B) {
for _, batchSize := range []int{10, 100, 1000, 10000} {
b.Run(fmt.Sprintf("Batch%d", batchSize), func(b *testing.B) {
b.Run("Words", func(b *testing.B) {
for i := 0; i < b.N; i++ {
src := data[:batchSize]
dst := buf64[:batchSize]
for len(src) >= 10 && len(dst) >= 10 {
dst[0] = src[9]
dst[1] = src[8]
dst[2] = src[7]
dst[3] = src[6]
dst[4] = src[5]
dst[5] = src[4]
dst[6] = src[3]
dst[7] = src[2]
dst[8] = src[1]
dst[9] = src[0]
src = src[10:]
dst = dst[10:]
}
}
})
b.Run("WordsAppend", func(b *testing.B) {
for i := 0; i < b.N; i++ {
src := data[:batchSize]
dst := buf64[:0]
for len(src) >= 10 {
dst = append(dst,
src[9],
src[8],
src[7],
src[6],
src[5],
src[4],
src[3],
src[2],
src[1],
src[0])
src = src[10:]
}
}
})
b.Run("Bytes", func(b *testing.B) {
for i := 0; i < b.N; i++ {
src := data[:batchSize]
dst := buf8[:batchSize*8]
for len(src) >= 10 && len(dst) >= 80 {
binary.LittleEndian.PutUint64(dst[0*8:], src[9])
binary.LittleEndian.PutUint64(dst[1*8:], src[8])
binary.LittleEndian.PutUint64(dst[2*8:], src[7])
binary.LittleEndian.PutUint64(dst[3*8:], src[6])
binary.LittleEndian.PutUint64(dst[4*8:], src[5])
binary.LittleEndian.PutUint64(dst[5*8:], src[4])
binary.LittleEndian.PutUint64(dst[6*8:], src[3])
binary.LittleEndian.PutUint64(dst[7*8:], src[2])
binary.LittleEndian.PutUint64(dst[8*8:], src[1])
binary.LittleEndian.PutUint64(dst[9*8:], src[0])
src = src[10:]
dst = dst[80:]
}
}
})
b.Run("BytesAppend", func(b *testing.B) {
for i := 0; i < b.N; i++ {
src := data[:batchSize]
dst := buf8[:0]
for len(src) >= 10 {
dst = binary.LittleEndian.AppendUint64(dst, src[9])
dst = binary.LittleEndian.AppendUint64(dst, src[8])
dst = binary.LittleEndian.AppendUint64(dst, src[7])
dst = binary.LittleEndian.AppendUint64(dst, src[6])
dst = binary.LittleEndian.AppendUint64(dst, src[5])
dst = binary.LittleEndian.AppendUint64(dst, src[4])
dst = binary.LittleEndian.AppendUint64(dst, src[3])
dst = binary.LittleEndian.AppendUint64(dst, src[2])
dst = binary.LittleEndian.AppendUint64(dst, src[1])
dst = binary.LittleEndian.AppendUint64(dst, src[0])
src = src[10:]
}
}
})
})
}
}
@pascaldekloe
Copy link
Author

goos: darwin
goarch: arm64
pkg: github.com/ronanh/intcomp/wordbench
BenchmarkShuffle64Bit/Batch10/Words-8   	430637913	         2.559 ns/op
BenchmarkShuffle64Bit/Batch10/WordsAppend-8         	491895675	         2.440 ns/op
BenchmarkShuffle64Bit/Batch10/Bytes-8               	357849769	         3.290 ns/op
BenchmarkShuffle64Bit/Batch10/BytesAppend-8         	289657186	         4.096 ns/op
BenchmarkShuffle64Bit/Batch100/Words-8              	63806452	        18.76 ns/op
BenchmarkShuffle64Bit/Batch100/WordsAppend-8        	66537442	        17.99 ns/op
BenchmarkShuffle64Bit/Batch100/Bytes-8              	46508398	        26.29 ns/op
BenchmarkShuffle64Bit/Batch100/BytesAppend-8        	32336260	        36.99 ns/op
BenchmarkShuffle64Bit/Batch1000/Words-8             	 6497124	       184.9 ns/op
BenchmarkShuffle64Bit/Batch1000/WordsAppend-8       	 6805016	       176.1 ns/op
BenchmarkShuffle64Bit/Batch1000/Bytes-8             	 4687851	       259.6 ns/op
BenchmarkShuffle64Bit/Batch1000/BytesAppend-8       	 3372344	       355.8 ns/op
BenchmarkShuffle64Bit/Batch10000/Words-8            	  484731	      2475 ns/op
BenchmarkShuffle64Bit/Batch10000/WordsAppend-8      	  489068	      2452 ns/op
BenchmarkShuffle64Bit/Batch10000/Bytes-8            	  444733	      2697 ns/op
BenchmarkShuffle64Bit/Batch10000/BytesAppend-8      	  340567	      3522 ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment