最近在做性能優化,有個函數里面的耗時特別長,看里面的操作大多是一些字符串拼接的操作,而字符串拼接在 golang 里面其實有很多種實現。
實現方法
1. 直接使用運算符
~~~
func BenchmarkAddStringWithOperator(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i < b.N; i++ {
_ = hello + "," + world
}
}
~~~
golang 里面的字符串都是不可變的,每次運算都會產生一個新的字符串,所以會產生很多臨時的無用的字符串,不僅沒有用,還會給 gc 帶來額外的負擔,所以性能比較差
2. fmt.Sprintf()
~~~
func BenchmarkAddStringWithSprintf(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%s,%s", hello, world)
}
}
~~~
內部使用 []byte 實現,不像直接運算符這種會產生很多臨時的字符串,但是內部的邏輯比較復雜,有很多額外的判斷,還用到了 interface,所以性能也不是很好
3. strings.Join()
~~~
func BenchmarkAddStringWithJoin(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i < b.N; i++ {
_ = strings.Join([]string{hello, world}, ",")
}
}
~~~
join會先根據字符串數組的內容,計算出一個拼接之后的長度,然后申請對應大小的內存,一個一個字符串填入,在已有一個數組的情況下,這種效率會很高,但是本來沒有,去構造這個數據的代價也不小
4. buffer.WriteString()
~~~
func BenchmarkAddStringWithBuffer(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i < 1000; i++ {
var buffer bytes.Buffer
buffer.WriteString(hello)
buffer.WriteString(",")
buffer.WriteString(world)
_ = buffer.String()
}
}
~~~
這個比較理想,可以當成可變字符使用,對內存的增長也有優化,如果能預估字符串的長度,還可以用 buffer.Grow() 接口來設置 capacity
測試結果
~~~
BenchmarkAddStringWithOperator-8 50000000 30.3 ns/op
BenchmarkAddStringWithSprintf-8 5000000 261 ns/op
BenchmarkAddStringWithJoin-8 30000000 58.7 ns/op
BenchmarkAddStringWithBuffer-8 2000000000 0.00 ns/op
~~~
這個是在我的自己 Mac 上面跑的結果,go 版本 go version go1.8 darwin/amd64,這個結果僅供參考,還是要以實際生產環境的值為準,代碼在:https://github.com/hatlonely/...
主要結論
在已有字符串數組的場合,使用 strings.Join() 能有比較好的性能
在一些性能要求較高的場合,盡量使用 buffer.WriteString() 以獲得更好的性能
性能要求不太高的場合,直接使用運算符,代碼更簡短清晰,能獲得比較好的可讀性
如果需要拼接的不僅僅是字符串,還有數字之類的其他需求的話,可以考慮 fmt.Sprintf
參考鏈接
go語言字符串拼接性能分析: http://herman.asia/efficient-...

- Go語言基礎篇
- Go語言簡介
- Go語言教程
- Go語言環境安裝
- Go語言結構
- Go語言基礎語法
- Go語言數據類型
- Go語言變量
- Go語言提高篇
- Go語言實現貪吃蛇
- Go 諺語
- 解決連通性問題的四種算法
- golang 幾種字符串的連接方式
- Go JSON 技巧
- Go += 包版本
- Golang 編譯成 DLL 文件
- Go指南:牛頓法開方
- Go語言異步服務器框架原理和實現
- Golang適合高并發場景的原因分析
- 如何設計并實現一個線程安全的 Map ?(上篇)
- go語言執行cmd命令關機、重啟等
- IT雜項
- IT 工程師的自我管理
- IT界不為人知的14個狗血故事
- Go語言版本說明
- Go 1.10中值得關注的幾個變化
- Golang面試題解析
- Golang面試題
- Golang語言web開發
- golang 模板(template)的常用基本語法
- go語言快速入門:template模板
- Go Template學習筆記
- LollipopGo框架
- 框架簡介
- Golang語言版本設計模式
- 設計模式-單例模式
- Golang語言資源下載
- 公眾賬號
- leaf
- 合作講師
- 公開課目錄