Created
August 17, 2016 16:32
-
-
Save kevinburke/b7aff2373dde66afdc0d17274cea7e8a to your computer and use it in GitHub Desktop.
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 lib | |
var s string | |
//go:noinline | |
func Join(a []string, sep string) string { | |
n := len(sep) * (len(a) - 1) | |
for i := 0; i < len(a); i++ { | |
n += len(a[i]) | |
} | |
zzzzzzzzzzzzzzzzzzzzzzzzzzz := make([]byte, n) | |
bp := copy(zzzzzzzzzzzzzzzzzzzzzzzzzzz, a[0]) | |
for _, s := range a[1:] { | |
bp += copy(zzzzzzzzzzzzzzzzzzzzzzzzzzz[bp:], sep) | |
bp += copy(zzzzzzzzzzzzzzzzzzzzzzzzzzz[bp:], s) | |
} | |
return string(zzzzzzzzzzzzzzzzzzzzzzzzzzz) | |
} | |
//go:noinline | |
func hi() string { | |
tojoin := []string{ | |
"a string to join with others", | |
"a second string", | |
} | |
return Join(tojoin, ",") | |
} |
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 lib | |
import ( | |
"strings" | |
"testing" | |
) | |
var sink string | |
func BenchmarkJoinOne(b *testing.B) { | |
tojoin := []string{"a string to join with others"} | |
for i := 0; i < b.N; i++ { | |
sink = strings.Join(tojoin, ",") | |
} | |
} | |
func BenchmarkJoinTwo(b *testing.B) { | |
tojoin := []string{"a string to join with others", "a second string"} | |
for i := 0; i < b.N; i++ { | |
sink = strings.Join(tojoin, ",") | |
} | |
} | |
func BenchmarkJoinThree(b *testing.B) { | |
tojoin := []string{"a string to join with others", "a second string", "a third string"} | |
for i := 0; i < b.N; i++ { | |
sink = strings.Join(tojoin, ",") | |
} | |
} | |
func BenchmarkJoinMany(b *testing.B) { | |
tojoin := []string{ | |
"a string to join with others", | |
"a second string", | |
"a third string", | |
"a fourth string that is quite a bit longer than the others", | |
"a fifth string", | |
"6", | |
"number seven", | |
"8", | |
"nine", | |
"the final string which is number ten", | |
} | |
for i := 0; i < b.N; i++ { | |
sink = strings.Join(tojoin, ",") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment