内置追加与字节。缓冲区写入

在我需要将未知数量的数据添加到一个字节的情况下,比如说在一个循环中,我可以使用内置函数append()或创建一个新函数Buffer并使用该Write()函数。

哪种方法最快?


慕标5832272
浏览 125回答 2
2回答

繁星淼淼

这取决于用例。在这两种情况下都比(样本:1、2、3、4)bytes.Buffer更快。append使用buf.Write(make([]byte, 16))需要4.6482659s,使用buf = append(buf, make([]byte, 16)...)需要6.6623811s。对于样本 5、6:使用buf = append(buf, byte(i))take 445.0255ms,Using buf.WriteByte(byte(i))take1.4410824s并bytes.Buffer使用内置函数copy,速度很快:// 写入将 p 的内容附加到缓冲区,根据需要增长缓冲区。返回值n是p的长度;错误总是零。如果// 缓冲区变得太大,Write 会出现 ErrTooLarge 恐慌。func (b *Buffer) Write(p []byte) (n int, err error) {&nbsp; b.lastRead = opInvalid&nbsp; m := b.grow(len(p))&nbsp; return copy(b.buf[m:], p), nil}bytes.Buffer耗时 4.8892797s,append耗时 7.7514434s查看这些基准:1-使用append:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; buf := []byte{}&nbsp; &nbsp; data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}&nbsp; &nbsp; t := time.Now()&nbsp; &nbsp; for i := 0; i < 100000000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buf = append(buf, data...)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Since(t))&nbsp; &nbsp; fmt.Println(len(buf))}输出:7.7514434s16000000002-使用bytes.Bufferpackage mainimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; buf := &bytes.Buffer{}&nbsp; &nbsp; data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}&nbsp; &nbsp; t := time.Now()&nbsp; &nbsp; for i := 0; i < 100000000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buf.Write(data)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Since(t))&nbsp; &nbsp; fmt.Println(buf.Len())}输出:4.8892797s16000000003-bytes.Buffer使用make([]byte, 16):package mainimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; buf := &bytes.Buffer{}&nbsp; &nbsp; t := time.Now()&nbsp; &nbsp; for i := 0; i < 100000000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buf.Write(make([]byte, 16))&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Since(t)) // 4.6482659s&nbsp; &nbsp; fmt.Println(buf.Len())&nbsp; &nbsp; &nbsp;//1600000000}4-append使用make([]byte, 16):package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; buf := []byte{}&nbsp; &nbsp; t := time.Now()&nbsp; &nbsp; for i := 0; i < 100000000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buf = append(buf, make([]byte, 16)...)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Since(t)) // 6.6623811s&nbsp; &nbsp; fmt.Println(len(buf))&nbsp; &nbsp; &nbsp; // 1600000000}5- 使用buf = append(buf, byte(i))需要445.0255ms:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; buf := []byte{}&nbsp; &nbsp; t := time.Now()&nbsp; &nbsp; for i := 0; i < 100000000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buf = append(buf, byte(i))&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Since(t)) // 445.0255ms&nbsp; &nbsp; fmt.Println(len(buf))&nbsp; &nbsp; &nbsp; // 100000000}6- 使用buf.WriteByte(byte(i))需要1.4410824s:package mainimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; buf := &bytes.Buffer{}&nbsp; &nbsp; t := time.Now()&nbsp; &nbsp; for i := 0; i < 100000000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteByte(byte(i))&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Since(t)) // 1.4410824s&nbsp; &nbsp; fmt.Println(buf.Len())&nbsp; &nbsp; &nbsp;// 100000000}

慕姐4208626

使用内置函数append更快,如以下基准所示:package ximport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "testing"&nbsp; &nbsp; "time")var startSeed = time.Now().UnixNano()func randomSlice() []byte {&nbsp; &nbsp; return make([]byte, 0, rand.Intn(1<<10))}func BenchmarkAppend(b *testing.B) {&nbsp; &nbsp; rand.Seed(startSeed)&nbsp; &nbsp; b.ResetTimer()&nbsp; &nbsp; var all []byte&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; all = append(all, randomSlice()...)&nbsp; &nbsp; }}func BenchmarkBufferWrite(b *testing.B) {&nbsp; &nbsp; rand.Seed(startSeed)&nbsp; &nbsp; b.ResetTimer()&nbsp; &nbsp; var buff bytes.Buffer&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buff.Write(randomSlice())&nbsp; &nbsp; }&nbsp; &nbsp; all := buff.Bytes()&nbsp; &nbsp; _ = all}结果:BenchmarkAppend-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10000000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;206 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;540 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/opBenchmarkBufferWrite-4&nbsp; &nbsp; &nbsp; 10000000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;214 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;540 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/op
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go