Last active
August 29, 2015 14:27
-
-
Save rhcarvalho/9ee6179e976dfde01153 to your computer and use it in GitHub Desktop.
comparing strings.Join to concatenating strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test | |
| import "strings" | |
| func stringjoin(key, namespace string) { | |
| _ = strings.Join([]string{namespace, "build.", key}, "") | |
| } | |
| func concat(key, namespace string) { | |
| _ = namespace + "build." + key | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ 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