字符串连接性能测试

网上有些字符串连接的性能测试,先看一个错误的

+连接

func BenchmarkAddStringWithOperator(b *testing.B) {
    hello := "hello"
    world := "world"
    for i := 0; i < b.N; i++ {
        _ = hello + "," + world
    }
}

测试结果

50000000            30.1 ns/op
PASS
ok      workspace/version   1.548s

fmt连接

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

测试结果

10000000           222 ns/op
PASS
ok      workspace/version   2.458s

join连接

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

测试结果

20000000            62.0 ns/op
PASS
ok      workspace/version   1.314s

bytes连接

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()
    }
}
20000000            91.0 ns/op
PASS
ok      workspace/version   1.925s

通过上面错误的测试其实很容易误导人,感觉是不是通过+连接就是最好的方式呢?其实上面的答案是错误的
错误的还不是一处两处。首先就是字符串+方式的拼接没有生成最终字符串,其次是buffer连接的时候每次生成
buffer对象等,都是降低性能的事情,下面看一个正确的测试的

package main



import (

    "bytes"

    "fmt"

    "strings"

    "time"

)



func benchmarkStringFunction(n int, index int) (d time.Duration) {

    v := "ni shuo wo shi bu shi tai wu liao le a?"

    var s string

    var buf bytes.Buffer



    t0 := time.Now()

    for i := 0; i < n; i++ {

        switch index {

        case 0: // fmt.Sprintf

            s = fmt.Sprintf("%s[%s]", s, v)

        case 1: // string +

            s = s + "[" + v + "]"

        case 2: // strings.Join

            s = strings.Join([]string{s, "[", v, "]"}, "")

        case 3: // temporary bytes.Buffer

            b := bytes.Buffer{}

            b.WriteString("[")

            b.WriteString(v)

            b.WriteString("]")

            s = b.String()

        case 4: // stable bytes.Buffer

            buf.WriteString("[")

            buf.WriteString(v)

            buf.WriteString("]")

        }



        if i == n-1 {

            if index == 4 { // for stable bytes.Buffer

                s = buf.String()

            }

            fmt.Println(len(s)) // consume s to avoid compiler optimization

        }

    }

    t1 := time.Now()

    d = t1.Sub(t0)

    fmt.Printf("time of way(%d)=%v\n", index, d)

    return d

}



func main() {

    k := 5

    d := [5]time.Duration{}

    for i := 0; i < k; i++ {

        d[i] = benchmarkStringFunction(100000, i)

    }



    for i := 0; i < k-1; i++ {

        fmt.Printf("way %d is %6.1f times of way %d\n", i, float32(d[i])/float32(d[k-1]), k-1)

    }

}

测试结果

410000
time of way(0)=354.647961ms
410000
time of way(1)=188.914254ms
410000
time of way(2)=532.93828ms
41
time of way(3)=1.386461ms
410000
time of way(4)=474.988µs
way 0 is  746.6 times of way 4
way 1 is  397.7 times of way 4
way 2 is 1122.0 times of way 4
way 3 is    2.9 times of way 4

这个里面充分显示了buffer连接的优势

猜你喜欢

转载自blog.csdn.net/u010278923/article/details/79650206
今日推荐