Go 字符串连接+=与strings.Join性能对比

Go字符串连接

对于字符串的连接大致有两种方式:

1、通过+号连接

func StrPlus1(a []string) string {
        var s, sep string
        for i := 0; i < len(a); i++ {
                s += sep + a[i]
                sep = " "
        }
        return s
}

2、通过strings.Join连接

func StrPlus2(a []string) string {
        return strings.Join(a, " ")
}

对比两种方式的效率,通过压力测试进行对比

import "testing"

func BenchmarkStrPlus1(b *testing.B) {
        for i := 0; i < b.N; i++ {
                StrPlus1([]string{"xxx", "bbb", "aaa"})
        }
}

func BenchmarkStrPlus2(b *testing.B) {
        for i := 0; i < b.N; i++ {
                StrPlus2([]string{"xxx", "bbb", "aaa"})
        }
}

运行压力测试go test -test.bench=".*"

goos: darwin
goarch: amd64
BenchmarkStrPlus1-4     10000000               127 ns/op
BenchmarkStrPlus2-4     20000000                78.7 ns/op

从本机来看通过+号连接字符串每个操作消耗127ns时间,strings.Join消耗78.7ns。效率上strings.Join更高

来看下strings包中Join的实现

// Join concatenates the elements of a to create a single string. The separator string
// sep is placed between elements in the resulting string.
func Join(a []string, sep string) string {
        switch len(a) {
        case 0:
                return ""
        case 1:
                return a[0]
        case 2:
                // Special case for common small values.
                // Remove if golang.org/issue/6714 is fixed
                return a[0] + sep + a[1]
        case 3:
                // Special case for common small values.
                // Remove if golang.org/issue/6714 is fixed
                return a[0] + sep + a[1] + sep + a[2]
        }
        n := len(sep) * (len(a) - 1)
        for i := 0; i < len(a); i++ {
                n += len(a[i])
        }

        b := make([]byte, n)
        bp := copy(b, a[0])
        for _, s := range a[1:] {
                bp += copy(b[bp:], sep)
                bp += copy(b[bp:], s)
        }
        return string(b)
}

可以看出当连接字符串数量较大时,是先通过make分配足够的空间,然后把每个字符串copy到空间里面,而不是每次通过+号来多次分配内存。