【Golang】golang 幾種字符串的連接方式

直接使用運算符
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 帶來額外的負擔,所以性能比較差。

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,所以性能也不是很好。

strings.Join()
func BenchmarkAddStringWithJoin(b *testing.B) {
    hello := "hello"
    world := "world"
    for i := 0; i < b.N; i++ {
        _ = strings.Join([]string{hello, world}, ",")
    }
}

join會先根據字符串數組的內容,計算出一個拼接之後的長度,然後申請對應大小的內存,一個一個字符串填入,在已有一個數組的情況下,這種效率會很高,但是本來沒有,去構造這個數據的代價也不小。

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  

主要結論

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

https://segmentfault.com/a/1190000012978989

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章