Last active
January 15, 2019 09:50
-
-
Save savsgio/c170e1643db03d5d03b892ab20b47068 to your computer and use it in GitHub Desktop.
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 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: