Go字符串拼接常见的方式有加号、fmt.Sprintf、strings.Builder、bytes.Buffer、strings.join、切片。
package concat_string
import (
"bytes"
"fmt"
"strconv"
"strings"
"testing"
)
const numbers = 100
// +
func BenchmarkStringAdd(b *testing.B) {
b.ResetTimer()
for idx := 0; idx < b.N; idx++ {
var s string
for i := 0; i < numbers; i++ {
s += strconv.Itoa(i)
}
}
b.StopTimer()
}
// fmt.Sprintf
func BenchmarkSprintf(b *testing.B) {
b.ResetTimer()
for idx := 0; idx < b.N; idx++ {
var s string
for i := 0; i < numbers; i++ {
s = fmt.Sprintf("%v%v", s, i)
}
}
b.StopTimer()
}
// strings.Builder
func BenchmarkStringBuilder(b *testing.B) {
b.ResetTimer()
for idx := 0; idx < b.N; idx++ {
var builder strings.Builder
for i := 0; i < numbers; i++ {
builder.WriteString(strconv.Itoa(i))
}
_ = builder.String()
}
b.StopTimer()
}
// bytes.Buffer
func BenchmarkBytesBuf(b *testing.B) {
b.ResetTimer()
for idx := 0; idx < b.N; idx++ {
var buf bytes.Buffer
for i := 0; i < numbers; i++ {
buf.WriteString(strconv.Itoa(i))
}
_ = buf.String()
}
b.StopTimer()
}
// strings.join
func BenchmarkStringsJoin(b *testing.B) {
b.ResetTimer()
var strs []string
for i := 0; i < b.N; i++ {
strs = append(strs, strconv.Itoa(i))
}
_ = strings.Join(strs, "")
b.StopTimer()
}
// 切片
func BenchmarkByteSliceString(b *testing.B) {
b.ResetTimer()
buf := make([]byte, 0)
for i := 0; i < b.N; i++ {
buf = append(buf, byte(i))
}
_ = string(buf)
b.StopTimer()
}
go test -bench=.
244851 4725 ns/op
表示单位时间内(默认是1s)被测函数运行了 244851次,每次运行耗时 4725ns
总耗时10.261s。
https://www.cnblogs.com/cheyunhua/p/15769717.html
http://t.zoukankan.com/yahuian-p-go-benchmark.html