Last active
April 2, 2023 13:41
-
-
Save pascaldekloe/9af7d2bb1a1bc88e162037bbc213f374 to your computer and use it in GitHub Desktop.
Go serialization speed test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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:] | |
} | |
} | |
}) | |
}) | |
} | |
} |
Author
pascaldekloe
commented
Apr 2, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment