Skip to content

Instantly share code, notes, and snippets.

@sysbot
Created May 10, 2018 23:12
Show Gist options
  • Select an option

  • Save sysbot/667602d485da3c9bff641aee5df01c4f to your computer and use it in GitHub Desktop.

Select an option

Save sysbot/667602d485da3c9bff641aee5df01c4f to your computer and use it in GitHub Desktop.
strings.Join vs fmt.Sprintf vs string concat (+)
package join
import (
"fmt"
"strings"
"testing"
)
var (
testData = []string{"a", "b", "c", "d", "e"}
)
func BenchmarkJoin(b *testing.B) {
for i := 0; i < b.N; i++ {
s := strings.Join(testData, ":")
_ = s
}
}
func BenchmarkSprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
s := fmt.Sprintf("%s:%s:%s:%s:%s", testData[0], testData[1], testData[2], testData[3], testData[4])
_ = s
}
}
func BenchmarkConcat(b *testing.B) {
for i := 0; i < b.N; i++ {
s := testData[0] + ":" + testData[1] + ":" + testData[2] + ":" + testData[3] + ":" + testData[4]
_ = s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment