Skip to content

Instantly share code, notes, and snippets.

@rhcarvalho
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save rhcarvalho/9ee6179e976dfde01153 to your computer and use it in GitHub Desktop.

Select an option

Save rhcarvalho/9ee6179e976dfde01153 to your computer and use it in GitHub Desktop.
comparing strings.Join to concatenating strings
package test
import "strings"
func stringjoin(key, namespace string) {
_ = strings.Join([]string{namespace, "build.", key}, "")
}
func concat(key, namespace string) {
_ = namespace + "build." + key
}
package test
import "testing"
func BenchmarkStringJoin(b *testing.B) {
for i := 0; i < b.N; i++ {
stringjoin("hello", "world")
}
}
func BenchmarkConcat(b *testing.B) {
for i := 0; i < b.N; i++ {
concat("hello", "world")
}
}
$ go test -bench .
testing: warning: no tests to run
PASS
BenchmarkStringJoin 10000000 210 ns/op
BenchmarkConcat 20000000 109 ns/op
ok _/tmp/pp 4.618s
$ # including memory allocation
$ go test -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkStringJoin 10000000 214 ns/op 32 B/op 2 allocs/op
BenchmarkConcat 20000000 110 ns/op 16 B/op 1 allocs/op
ok _/tmp/pp 4.707s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment