Skip to content

Instantly share code, notes, and snippets.

@brianfoshee
Created June 24, 2016 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianfoshee/c6c4e22c2bbc2ccd3768653e27ea0106 to your computer and use it in GitHub Desktop.
Save brianfoshee/c6c4e22c2bbc2ccd3768653e27ea0106 to your computer and use it in GitHub Desktop.
string concat vs Sprintf bench
package main
import (
"fmt"
"testing"
)
var result string
func BenchmarkSprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
result = fmt.Sprintf("%s, %s", "hello", "world")
}
}
func BenchmarkStringConcat(b *testing.B) {
for i := 0; i < b.N; i++ {
result = "hello" + ", " + "world"
}
}
@brianfoshee
Copy link
Author

BenchmarkSprintf-4       5000000               325 ns/op
BenchmarkStringConcat-4 2000000000               0.70 ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment