现在我正在使用下面的代码(如 中所示BenchmarkEncoder())并且速度很快,但我想知道是否有更快、更有效的方法。我用GOMAXPROCS=1和作为基准:
sudo -E nice -n -20 go test -bench . -benchmem -benchtime 3s
.
package blackbird
import (
"testing"
"encoding/hex"
"log"
"bytes"
"encoding/json"
)
var (
d1, d2, d3, d4, outBytes []byte
toEncode [][]byte
)
func init() {
var err interface{}
d1, err = hex.DecodeString("6e5438fd9c3748868147d7a4f6d355dd")
d2, err = hex.DecodeString("0740e2dfa4b049f2beeb29cc304bdb5f")
d3, err = hex.DecodeString("ab6743272358467caff7d94c3cc58e8c")
d4, err = hex.DecodeString("7411c080762a47f49e5183af12d87330e6d0df7dd63a44808db4e250cdea0a36182fce4a309842e49f4202eb90184dd5b621d67db4a04940a29e981a5aea59be")
if err != nil {
log.Fatal("hex decoding failed: %v", err)
}
toEncode = [][]byte{d1, d2, d3, d4}
}
func Encode(stuff [][]byte) []byte {
return bytes.Join(stuff, nil)
}
func BenchmarkEncoderDirect(b *testing.B) {
for i := 0; i < b.N; i++ {
bytes.Join(toEncode, nil)
}
}
func BenchmarkEncoder(b *testing.B) {
for i := 0; i < b.N; i++ {
Encode(toEncode)
}
}
func BenchmarkJsonEncoder(b *testing.B) {
for i := 0; i < b.N; i++ {
outBytes, _ = json.Marshal(toEncode)
}
}
将多个连接[]byte在一起的最快方法是什么?
jeck猫
慕标5832272
相关分类