quipo / statsd

Golang StatsD client
MIT License
164 stars 51 forks source link

Optimize: Replacing string formatting with concatenation #43

Closed agnivade closed 7 years ago

agnivade commented 7 years ago

Benchmarks show clear improvement -


func BenchmarkStringSprintf(b *testing.B) {
    w1 := "hello"
    w2 := "world"
    w3 := "fd"
    for n := 0; n < b.N; n++ {
        fmt.Sprintf("%s%s:%s", w1, w2, w3)
    }
}

func BenchmarkStringConcat(b *testing.B) {
    w1 := "hello"
    w2 := "world"
    w3 := "fd"
    for n := 0; n < b.N; n++ {
        _ = w1 + w2 + ":" + w3
    }
}
BenchmarkStringSprintf-4     5000000           338 ns/op          64 B/op          4 allocs/op
BenchmarkStringConcat-4     30000000            40.3 ns/op         0 B/op          0 allocs/op
PASS
ok      stdtest 3.303s
agnivade commented 7 years ago

Hi @quipo - Any updates on this ? Thanks.

quipo commented 7 years ago

Thanks @agnivade!