golang 幾種字符串的連接方式
最近在做性能優化,有個函數里面的耗時特別長,看里面的操作大多是一些字符串拼接的操作,而字符串拼接在 golang 里面其實有很多種實現。
實現方法
1. 直接使用運算符o
func BenchmarkAddStringWithOperator(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i < b.N; i++ {
_ = hello + "," + world
}
}
func BenchmarkAddMoreStringWithOperator(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i < b.N; i++ {
var str string
for i := 0; i < 100; i++ {
str += 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 < b.N; i++ {
var buffer bytes.Buffer
buffer.WriteString(hello)
buffer.WriteString(",")
buffer.WriteString(world)
_ = buffer.String()
}
}
func BenchmarkAddMoreStringWithBuffer(b *testing.B) {
hello := "hello"
world := "world"
for i := 0; i < b.N; i++ {
var buffer bytes.Buffer
for i := 0; i < 100; i++ {
buffer.WriteString(hello)
buffer.WriteString(",")
buffer.WriteString(world)
}
_ = buffer.String()
}
}
這個比較理想,可以當成可變字符使用,對內存的增長也有優化,如果能預估字符串的長度,還可以用 buffer.Grow()
接口來設置 capacity
測試結果
BenchmarkAddStringWithOperator-8 50000000 28.4 ns/op 0 B/op 0 allocs/op
BenchmarkAddStringWithSprintf-8 10000000 234 ns/op 48 B/op 3 allocs/op
BenchmarkAddStringWithJoin-8 30000000 56.2 ns/op 16 B/op 1 allocs/op
BenchmarkAddStringWithBuffer-8 20000000 86.0 ns/op 112 B/op 1 allocs/op
BenchmarkAddMoreStringWithOperator-8 100000 14295 ns/op 58896 B/op 100 allocs/op
BenchmarkAddMoreStringWithBuffer-8 300000 4551 ns/op 5728 B/op 7 allocs/op
這個是在我的自己 Mac 上面跑的結果,go 版本 go version go1.8 darwin/amd64
,這個結果僅供參考,還是要以實際生產環境的值為准,代碼在:https://github.com/hatlonely/hellogolang/blob/master/internal/buildin/string_test.go
主要結論
- 在已有字符串數組的場合,使用
strings.Join()
能有比較好的性能 - 在一些性能要求較高的場合,盡量使用
buffer.WriteString()
以獲得更好的性能 - 較少字符串連接的場景下性能最好,而且代碼更簡短清晰,可讀性更好
- 如果需要拼接的不僅僅是字符串,還有數字之類的其他需求的話,可以考慮
fmt.Sprintf
參考鏈接
go語言字符串拼接性能分析: http://herman.asia/efficient-string-concatenation-in-go