Skip to content

Instantly share code, notes, and snippets.

@tdakkota
Created June 30, 2021 14:31
Show Gist options
  • Save tdakkota/a8eb1d12a872d04b3eaf706e286b64dc to your computer and use it in GitHub Desktop.
Save tdakkota/a8eb1d12a872d04b3eaf706e286b64dc to your computer and use it in GitHub Desktop.
повезло повезло
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkEncode
BenchmarkEncode/Original
BenchmarkEncode/Original-12 198959738 6.091 ns/op 0 B/op 0 allocs/op
BenchmarkEncode/Inline
BenchmarkEncode/Inline-12 252104388 4.651 ns/op 0 B/op 0 allocs/op
BenchmarkEncode/Expand
BenchmarkEncode/Expand-12 137883550 8.575 ns/op 0 B/op 0 allocs/op
BenchmarkEncode/Optimized
BenchmarkEncode/Optimized-12 320572156 3.725 ns/op 0 B/op 0 allocs/op
PASS
package blyat
import (
"encoding/binary"
"testing"
"github.com/gotd/td/bin"
)
func BenchmarkEncode(b *testing.B) {
var (
TypeID uint32 = 1
ID int = 10
AccessHash int64 = 10
)
b.Run("Original", func(b *testing.B) {
buf := bin.Buffer{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.PutID(TypeID)
buf.PutInt(ID)
buf.PutLong(AccessHash)
}
})
b.Run("Inline", func(b *testing.B) {
buf := bin.Buffer{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.Reset()
{
t := make([]byte, bin.Word)
binary.LittleEndian.PutUint32(t, TypeID)
buf.Buf = append(buf.Buf, t...)
}
{
t := make([]byte, bin.Word)
binary.LittleEndian.PutUint32(t, uint32(ID))
buf.Buf = append(buf.Buf, t...)
}
{
t := make([]byte, 2*bin.Word)
binary.LittleEndian.PutUint64(t, uint64(AccessHash))
buf.Buf = append(buf.Buf, t...)
}
}
})
b.Run("Expand", func(b *testing.B) {
buf := bin.Buffer{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.Expand(4)
binary.LittleEndian.PutUint32(buf.Buf[0:4:4], TypeID)
buf.Expand(4)
binary.LittleEndian.PutUint32(buf.Buf[4:8:8], uint32(ID))
buf.Expand(8)
binary.LittleEndian.PutUint64(buf.Buf[8:16:16], uint64(AccessHash))
}
})
b.Run("Optimized", func(b *testing.B) {
buf := bin.Buffer{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.Expand(4 + 4 + 8)
binary.LittleEndian.PutUint32(buf.Buf[0:4:4], TypeID)
binary.LittleEndian.PutUint32(buf.Buf[4:8:8], uint32(ID))
binary.LittleEndian.PutUint64(buf.Buf[8:16:16], uint64(AccessHash))
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment