连接字符串的几种方式

Published Fri 09 February 2018 in Golang

by HanXiao  


转自 hatlonely Golang 语言社区

最近在做性能优化, 有个函数里面的耗时特别长, 看里面的操作大多是一些字符串拼接的操作, 而字符串拼接在 golang 里面其实有很多种实现.

直接使用运算符

func BenchmarkAddStringWithOperator(b *testing.B) {
    hello := "hello"
    world := "world"
    for i := 0; i < b.N; i++ {
        _ = 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 < 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, 这个结果仅供参考, 还是要以实际生产环境的值为准.

主要结论

  • 在已有字符串数组的场合, 使用 strings.Join() 能有比较好的性能
  • 在一些性能要求较高的场合, 尽量使用 buffer.WriteString() 以获得更好的性能
  • 性能要求不太高的场合, 直接使用运算符, 代码更简短清晰, 能获得比较好的可读性
  • 如果需要拼接的不仅仅是字符串, 还有数字之类的其他需求的话, 可以考虑 fmt.Sprintf()