|
|
|
|
|
by tapirl
3032 days ago
|
|
I would mention that, gc (the official Go compiler) makes special optimization for string concatenation operation (+). If the number of strings to be concatenated is known at compile time, using + to concatenate strings is the most efficient. package a
import "testing"
import "strings"
var strA, strB string
var x, y, z = "x", "y", "z"
func BenchmarkConcatString(b *testing.B) {
for n := 0; n < b.N; n++ {
strA = x + y + z
}
}
func BenchmarkConcatBuilder(b *testing.B) {
for n := 0; n < b.N; n++ {
var builder strings.Builder
builder.WriteString(x)
builder.WriteString(y)
builder.WriteString(z)
strB = builder.String()
}
}
Result: goos: linux
goarch: amd64
BenchmarkConcatString-2 20000000 83.7 ns/op
BenchmarkConcatBuilder-2 20000000 102 ns/op
|
|
Perhaps the use of += as separate statements is the difference, but one would hope that gc wasn't so fragile as to be unable to identify those sequences as identical.
---
[1] https://news.ycombinator.com/item?id=16533650