Skip to content

Instantly share code, notes, and snippets.

@savsgio
Last active January 15, 2019 09:50
Show Gist options
  • Save savsgio/c170e1643db03d5d03b892ab20b47068 to your computer and use it in GitHub Desktop.
Save savsgio/c170e1643db03d5d03b892ab20b47068 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"log"
"reflect"
"testing"
"unsafe"
"github.com/savsgio/gotils"
"github.com/valyala/bytebufferpool"
)
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func s2b(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}
func init() {
buf := bytes.NewBuffer(nil)
log.SetOutput(buf)
}
func BenchmarkS2B_std(b *testing.B) {
str := "Hola amigo mio como te va la vida jejejej"
var bt []byte
b.ResetTimer()
for i := 0; i < b.N; i++ {
bt = []byte(str)
}
b.StopTimer()
log.Println(bt)
}
func BenchmarkS2B_bytesbufferpool(b *testing.B) {
str := "Hola amigo mio como te va la vida jejejej"
b.ResetTimer()
for i := 0; i < b.N; i++ {
buff := bytebufferpool.Get()
buff.SetString(str)
bytebufferpool.Put(buff)
}
}
func BenchmarkS2B_gotils(b *testing.B) {
str := "Hola amigo mio como te va la vida jejejej"
b.ResetTimer()
for i := 0; i < b.N; i++ {
gotils.S2B(str)
}
}
func BenchmarkS2B_internal(b *testing.B) {
str := "Hola amigo mio como te va la vida jejejej"
b.ResetTimer()
for i := 0; i < b.N; i++ {
s2b(str)
}
}
func BenchmarkB2S_std(b *testing.B) {
bt := []byte("Hola amigo mio como te va la vida jejejej")
var str string
b.ResetTimer()
for i := 0; i < b.N; i++ {
str = string(bt)
}
b.StopTimer()
log.Println(str)
}
func BenchmarkB2S_bytesbufferpool(b *testing.B) {
bt := []byte("Hola amigo mio como te va la vida jejejej")
b.ResetTimer()
for i := 0; i < b.N; i++ {
buff := bytebufferpool.Get()
buff.Set(bt)
bytebufferpool.Put(buff)
}
}
func BenchmarkB2S_gotils(b *testing.B) {
bt := []byte("Hola amigo mio como te va la vida jejejej")
b.ResetTimer()
for i := 0; i < b.N; i++ {
gotils.B2S(bt)
}
}
func BenchmarkB2S_internal(b *testing.B) {
bt := []byte("Hola amigo mio como te va la vida jejejej")
b.ResetTimer()
for i := 0; i < b.N; i++ {
b2s(bt)
}
}
@savsgio
Copy link
Author

savsgio commented Oct 9, 2018

Result:

goos: linux
goarch: amd64

BenchmarkS2B_std-4                      20000000                61.4 ns/op            48 B/op          1 allocs/op
BenchmarkS2B_bytesbufferpool-4          30000000                46.5 ns/op             0 B/op          0 allocs/op
BenchmarkS2B_gotils-4                   2000000000               0.38 ns/op            0 B/op          0 allocs/op
BenchmarkS2B_internal-4                 2000000000               0.39 ns/op            0 B/op          0 allocs/op

BenchmarkB2S_std-4                      30000000                50.7 ns/op            48 B/op          1 allocs/op
BenchmarkB2S_bytesbufferpool-4          30000000                45.1 ns/op             0 B/op          0 allocs/op
BenchmarkB2S_gotils-4                   2000000000               0.37 ns/op            0 B/op          0 allocs/op
BenchmarkB2S_internal-4                 2000000000               0.38 ns/op            0 B/op          0 allocs/op

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