zoukankan      html  css  js  c++  java
  • golang 几种字符串的连接方式

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

    实现方法

    1. 直接使用运算符

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

    golang 里面的字符串都是不可变的,每次运算都会产生一个新的字符串,所以会产生很多临时的无用的字符串,不仅没有用,还会给 gc 带来额外的负担,所以性能比较差

    2. fmt.Sprintf()

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

    内部使用 []byte 实现,不像直接运算符这种会产生很多临时的字符串,但是内部的逻辑比较复杂,有很多额外的判断,还用到了 interface,所以性能也不是很好

    3. strings.Join()

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

    join会先根据字符串数组的内容,计算出一个拼接之后的长度,然后申请对应大小的内存,一个一个字符串填入,在已有一个数组的情况下,这种效率会很高,但是本来没有,去构造这个数据的代价也不小

    4. buffer.WriteString()

    1. func BenchmarkAddStringWithBuffer(b *testing.B) {
    2. hello := "hello"
    3. world := "world"
    4. for i := 0; i < 1000; i++ {
    5. var buffer bytes.Buffer
    6. buffer.WriteString(hello)
    7. buffer.WriteString(",")
    8. buffer.WriteString(world)
    9. _ = buffer.String()
    10. }
    11. }

    这个比较理想,可以当成可变字符使用,对内存的增长也有优化,如果能预估字符串的长度,还可以用 buffer.Grow() 接口来设置 capacity

    测试结果

    1. BenchmarkAddStringWithOperator-8 50000000 30.3 ns/op
    2. BenchmarkAddStringWithSprintf-8 5000000 261 ns/op
    3. BenchmarkAddStringWithJoin-8 30000000 58.7 ns/op
    4. BenchmarkAddStringWithBuffer-8 2000000000 0.00 ns/op

    这个是在我的自己 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

    转载请注明出处 
    本文链接:http://hatlonely.github.io/2018/01/24/golang字符串的几种连接方式/

  • 相关阅读:
    Masscan入门手册
    Deepin安装Python3
    docker 配置 kafka+zookeeper,golang操作kafka
    VMware安装Centos7超详细过程(图文)
    国家代码查询
    thinkphp5 关于跨域的一些坑,附上解决办法(比较全面了)
    网络协议分析与抓包 TCP/IP UDP等
    一维数组分组成二维数组
    Fiddler在抓取https数据包时如何解决Tunnel to 443的问题?
    十条nmap常用的扫描命令
  • 原文地址:https://www.cnblogs.com/hatlonely/p/8343879.html
Copyright © 2011-2022 走看看