Skip to content

Instantly share code, notes, and snippets.

@ysinjab
Created February 26, 2023 14:49
Show Gist options
  • Save ysinjab/9cdfb04dd63e32105b7cf5d41c9495f0 to your computer and use it in GitHub Desktop.
Save ysinjab/9cdfb04dd63e32105b7cf5d41c9495f0 to your computer and use it in GitHub Desktop.
package concatenate_test
import (
"strings"
"testing"
)
func Concatenate(texts []string) string {
s := ""
for _, value := range texts {
s += value
}
return s
}
func ConcatenateUsingStringsBuilderWithoutGrow(texts []string) string {
sb := strings.Builder{}
for _, v := range texts {
_, _ = sb.WriteString(v)
}
return sb.String()
}
func ConcatenateUsingStringsBuilderWithGrow(texts []string) string {
count := 0
for i := 0; i < len(texts); i++ {
count += len(texts[i])
}
sb := strings.Builder{}
sb.Grow(count)
for _, v := range texts {
_, _ = sb.WriteString(v)
}
return sb.String()
}
func BenchmarkConcatenate(b *testing.B) {
for i := 0; i < b.N; i++ {
Concatenate([]string{"The", "sea", "is", "a", "vast", "and", "mysterious", "place,", "full", "of", "life", "and", "beauty.", "Its", "depths", "are", "filled", "with", "untold", "secrets", "and", "wonders,", "and", "its", "surface", "is", "an", "ever-changing", "canvas", "of", "blues,", "greens,", "and", "greys.", "From", "the", "gentle", "lapping", "of", "the", "waves", "to", "the", "powerful", "roar", "of", "the", "ocean,", "the", "sea", "is", "a", "source", "of", "endless", "fascination", "and", "delight.", "Its", "power", "and", "beauty", "have", "inspired", "poets,", "writers,", "and", "artists", "for", "centuries,", "and", "its", "mysteries", "still", "captivate", "us", "today.", "Whether", "youre", "relaxing", "on", "a", "beach", "or", "exploring", "the", "depths", "of", "the", "ocean,", "the", "sea", "is", "sure", "to", "bring", "a", "sense", "of", "awe", "and", "wonder."})
}
}
func BenchmarkConcatenateUsingStringsBuilderWithoutGrow(b *testing.B) {
for i := 0; i < b.N; i++ {
ConcatenateUsingStringsBuilderWithoutGrow([]string{"The", "sea", "is", "a", "vast", "and", "mysterious", "place,", "full", "of", "life", "and", "beauty.", "Its", "depths", "are", "filled", "with", "untold", "secrets", "and", "wonders,", "and", "its", "surface", "is", "an", "ever-changing", "canvas", "of", "blues,", "greens,", "and", "greys.", "From", "the", "gentle", "lapping", "of", "the", "waves", "to", "the", "powerful", "roar", "of", "the", "ocean,", "the", "sea", "is", "a", "source", "of", "endless", "fascination", "and", "delight.", "Its", "power", "and", "beauty", "have", "inspired", "poets,", "writers,", "and", "artists", "for", "centuries,", "and", "its", "mysteries", "still", "captivate", "us", "today.", "Whether", "youre", "relaxing", "on", "a", "beach", "or", "exploring", "the", "depths", "of", "the", "ocean,", "the", "sea", "is", "sure", "to", "bring", "a", "sense", "of", "awe", "and", "wonder."})
}
}
func BenchmarkConcatenateUsingStringsBuilderWithGrow(b *testing.B) {
for i := 0; i < b.N; i++ {
ConcatenateUsingStringsBuilderWithGrow([]string{"The", "sea", "is", "a", "vast", "and", "mysterious", "place,", "full", "of", "life", "and", "beauty.", "Its", "depths", "are", "filled", "with", "untold", "secrets", "and", "wonders,", "and", "its", "surface", "is", "an", "ever-changing", "canvas", "of", "blues,", "greens,", "and", "greys.", "From", "the", "gentle", "lapping", "of", "the", "waves", "to", "the", "powerful", "roar", "of", "the", "ocean,", "the", "sea", "is", "a", "source", "of", "endless", "fascination", "and", "delight.", "Its", "power", "and", "beauty", "have", "inspired", "poets,", "writers,", "and", "artists", "for", "centuries,", "and", "its", "mysteries", "still", "captivate", "us", "today.", "Whether", "youre", "relaxing", "on", "a", "beach", "or", "exploring", "the", "depths", "of", "the", "ocean,", "the", "sea", "is", "sure", "to", "bring", "a", "sense", "of", "awe", "and", "wonder."})
}
}
// $ go test -bench=.
// goos: darwin
// goarch: amd64
// pkg: my_go/concatenate
// cpu: VirtualApple @ 2.50GHz
// BenchmarkConcatenate-10 143859 7081 ns/op
// BenchmarkConcatenateUsingStringsBuilderWithoutGrow-10 1412930 853.5 ns/op
// BenchmarkConcatenateUsingStringsBuilderWithGrow-10 2135326 558.9 ns/op
// PASS
// ok my_go/concatenate 5.457s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment