golang 字符串拼接性能的對比分析
背景
最近在做一個服務(wù)發(fā)現(xiàn)/注冊的agent, 各個服務(wù)需要通過這個agent來注冊自己的服務(wù),在完成
開發(fā)后,測試性能時發(fā)現(xiàn)性能達(dá)不到要求,通過pprof 來確認(rèn)cpu主要耗費在gc上,分析結(jié)果主要是由于字符串拼接導(dǎo)致,故需要測試一下字符串拼接的幾種方法的性能;
字符串拼接的幾種方法
1、直接使用加號進(jìn)行拼接
2、strings.Join()
3、fmt.Sprintf()
4、bytes.Buffer
大量字符串拼接性能測試
我們使用的場景主要是大量字符串拼接,所以需要的場景是不斷在字符串上拼接所以測試函數(shù)如下:
// fmt.Printf
func BenchmarkFmtSprintfMore(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
s += fmt.Sprintf("%s%s", "hello", "world")
}
fmt.Errorf(s)
}
// 加號 拼接
func BenchmarkAddMore(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
s += "hello" + "world"
}
fmt.Errorf(s)
}
// strings.Join
func BenchmarkStringsJoinMore(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
s += strings.Join([]string{"hello", "world"}, "")
}
fmt.Errorf(s)
}
// bytes.Buffer
func BenchmarkBufferMore(b *testing.B) {
buffer := bytes.Buffer{}
for i := 0; i < b.N; i++ {
buffer.WriteString("hello")
buffer.WriteString("world")
}
fmt.Errorf(buffer.String())
}
執(zhí)行測試函數(shù)
~/gopath/src/test/string go test -bench="." goos: darwin goarch: amd64 pkg: test/string BenchmarkFmtSprintfMore-4 300000 118493 ns/op BenchmarkAddMore-4 300000 124940 ns/op BenchmarkStringsJoinMore-4 300000 117050 ns/op BenchmarkBufferMore-4 100000000 37.2 ns/op PASS ok test/string 112.294s
從上可以看出使用bytes.buffer的性能是非常高的,如果涉及到大量數(shù)據(jù)拼接推薦
bytes.buffer{}
單次字符串拼接性能測試
func BenchmarkFmtSprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
s := fmt.Sprintf("%s%s", "hello", "world")
fmt.Errorf(s)
}
}
func BenchmarkAdd(b *testing.B) {
for i := 0; i < b.N; i++ {
s := "hello" + "world"
fmt.Errorf(s)
}
}
func BenchmarkStringsJoin(b *testing.B) {
for i := 0; i < b.N; i++ {
s := strings.Join([]string{"hello", "world"}, "")
fmt.Errorf(s)
}
}
func BenchmarkBuffer(b *testing.B) {
for i := 0; i < b.N; i++ {
b := bytes.Buffer{}
b.WriteString("hello")
b.WriteString("world")
fmt.Errorf(b.String())
}
}
執(zhí)行測試函數(shù)
~/gopath/src/test/string go test -bench="." goos: darwin goarch: amd64 pkg: test/string BenchmarkFmtSprintf-4 10000000 200 ns/op BenchmarkAdd-4 20000000 93.6 ns/op BenchmarkStringsJoin-4 10000000 152 ns/op BenchmarkBuffer-4 10000000 175 ns/op PASS ok test/string 7.818s
從上可以看出單詞調(diào)用字符串拼接性能 + > strings.Join > bytes.Buffer > fmt.Sprintf
總結(jié)
如果涉及到大量數(shù)據(jù)拼接推薦 bytes.buffer{}
后記
當(dāng)然只是通過bytes.buffer{} 性能還是不夠的,針對這個問題我們通過添加緩存進(jìn)一步接口qps.
cpu 耗費在gc 上的原因,需要分析golang gc 回收機(jī)制, 這是另外一個topic, 之后總結(jié)。
補(bǔ)充:Go語言字符串批量拼接-StringBuilder
1.安裝
go get -u github.com/typa01/go-utils
import ( "github.com/typa01/go-utils" )
使用,例:
fieldNames := tsgutils.NewInterfaceBuilder()
2.使用
func TestStringBuilderReplace(t *testing.T) {
builder1 := NewStringBuilder()
builder1.Append("%111%abc%987%")
FmtPrintln(builder1.Replace("%", "$").ToString()) // $111$abc$987$
builder2 := builder1.Clear()
builder2.AppendStrings("abc","defg").AppendInt(123).AppendFloat64(66.44).AppendStrings("aaa", "bbb", "&")
FmtPrintln(builder2.RemoveLast().ToString()) // abcdefg1236.644E+01aaabbb
str1 := NewString("123")
builder3 := NewStringBuilderString(str1).Append("456")
FmtPrintln(builder3.ToString()) // 123456
}
3.GitHub源碼地址
https://github.com/typa01/go-utils
https://github.com/typa01/go-utils/blob/master/string_builder.go
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
詳解go-micro微服務(wù)consul配置及注冊中心
這篇文章主要為大家介紹了go-micro微服務(wù)consul配置及注冊中心示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Go語言metrics應(yīng)用監(jiān)控指標(biāo)基本使用說明
這篇文章主要為大家介紹了Go語言metrics應(yīng)用監(jiān)控指標(biāo)的基本使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
golang服務(wù)報錯:?write:?broken?pipe的解決方案
在開發(fā)在線客服系統(tǒng)的時候,看到日志里有一些錯誤信息,下面這篇文章主要給大家介紹了關(guān)于golang服務(wù)報錯:?write:?broken?pipe的解決方案,需要的朋友可以參考下2022-09-09
Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式
這篇文章主要介紹了Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
golang網(wǎng)絡(luò)socket粘包問題的解決方法
這篇文章主要介紹了golang網(wǎng)絡(luò)socket粘包問題的解決方法,簡單講述了socket粘包的定義并結(jié)合實例形式分析了Go語言解決粘包問題的方法,需要的朋友可以參考下2016-07-07

