golang 幾種字符串的拼接方式


最近在做性能優化,有個函數里面的耗時特別長,看里面的操作大多是一些字符串拼接的操作,而字符串拼接在 golang 里面其實有很多種實現。

實現方法

1.直接使用運算符

func BenchmarkAddStringWithOperator(b *testing.B) { hello := "hello" world := "world" for i := 0; i < b.N; i++ { _ = hello + "," + world } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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) } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

內部使用 []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}, ",") } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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() } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

這個比較理想,可以當成可變字符使用,對內存的增長也有優化,如果能預估字符串的長度,還可以用 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
  • 1
  • 2
  • 3
  • 4

這個是在我的自己 Mac 上面跑的結果,go 版本 go version go1.8 darwin/amd64,這個結果僅供參考,還是要以實際生產環境的值為准,代碼在:https://github.com/hatlonely/hellogolang/blob/master/internal/buildin/string_test.go

主要結論

  1. 在已有字符串數組的場合,使用 strings.Join() 能有比較好的性能
  2. 在一些性能要求較高的場合,盡量使用 buffer.WriteString() 以獲得更好的性能
  3. 性能要求不太高的場合,直接使用運算符,代碼更簡短清晰,能獲得比較好的可讀性
  4. 如果需要拼接的不僅僅是字符串,還有數字之類的其他需求的話,可以考慮 fmt.Sprintf()

參考鏈接

go語言字符串拼接性能分析: http://herman.asia/efficient-string-concatenation-in-go

本文轉載自簡書文章:golang 幾種字符串的連接方式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM