package mytest

import (
	"bytes"
	"fmt"
	"strconv"
	"strings"
	"testing"
)

const NUMBERS = 10000

func BenchmarkStringSprintf(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		var str string
		for j := 0; j < NUMBERS; j++ {
			str = fmt.Sprintf("%s%d", str, j)
		}
	}
	b.StopTimer()
}
func BenchmarkStringAdd(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		var str string
		for j := 0; j < NUMBERS; j++ {
			str = str + strconv.Itoa(j)
		}
	}
	b.StopTimer()
}

func BenchmarkStringStringBuilder(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		var str string
		var builder strings.Builder
		for j := 0; j < NUMBERS; j++ {
			builder.WriteString(strconv.Itoa(j))
			builder.WriteString(str)
		}
		_ = builder.String()
	}
	b.StopTimer()
}

func BenchmarkStringbuffer(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		var str string
		var buf bytes.Buffer
		for j := 0; j < NUMBERS; j++ {
			buf.WriteString(strconv.Itoa(j))
			buf.WriteString(str)
		}
		_ = buf.String()
	}
	b.StopTimer()
}

func BenchmarkStringBytes(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		var str string
		buf := make([]byte, 0, NUMBERS*len(str))
		for i := 0; i < NUMBERS; i++ {
			buf = append(buf, str...)
		}
		_ = string(buf)
	}
	b.StopTimer()
}
测试

Golang字符串拼接性能测试_拼接字符串

总结

strings.Builderbytes.Buffer 和 []byte 的性能差距不大,而且消耗的内存也十分接近,性能最好且消耗内存最小的是 preByteConcat,这种方式预分配了内存,在字符串拼接的过程中,不需要进行字符串的拷贝,也不需要分配新的内存,因此性能最好,且内存消耗最小。综合易用性和性能,一般推荐使用 strings.Builder 来拼接字符串。