Skip to content

Instantly share code, notes, and snippets.

@florianl
Last active April 27, 2026 06:09
Show Gist options
  • Select an option

  • Save florianl/2192a408ad3cf84649f0d256f7a1d882 to your computer and use it in GitHub Desktop.

Select an option

Save florianl/2192a408ad3cf84649f0d256f7a1d882 to your computer and use it in GitHub Desktop.
compare fmt.Sprint with strings.Join
package main
import (
"cmp"
"fmt"
"slices"
"strings"
"testing"
)
// Version 1: using strings.Join with comma separator
func asSortedStringJoin[T cmp.Ordered](input []T) string {
if len(input) == 0 {
return ""
}
slices.Sort(input)
strValues := make([]string, len(input))
for i, v := range input {
strValues[i] = fmt.Sprint(v)
}
return strings.Join(strValues, ",")
}
// Version 2: using fmt.Sprint on the slice
func asSortedStringSprint[T cmp.Ordered](input []T) string {
if len(input) == 0 {
return ""
}
slices.Sort(input)
strValues := make([]string, len(input))
for i, v := range input {
strValues[i] = fmt.Sprint(v)
}
return fmt.Sprint(strValues)
}
// Version 3: using fmt.Sprint without extra slice
func asSortedStringShortSprint[T cmp.Ordered](input []T) string {
if len(input) == 0 {
return ""
}
slices.Sort(input)
return fmt.Sprint(input)
}
// Benchmark with small input (10 elements)
func BenchmarkJoinSmall(b *testing.B) {
input := []int{5, 2, 8, 1, 9, 3, 7, 4, 6, 0}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringJoin(input)
}
}
func BenchmarkSprintSmall(b *testing.B) {
input := []int{5, 2, 8, 1, 9, 3, 7, 4, 6, 0}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringSprint(input)
}
}
func BenchmarkShortSprintSmall(b *testing.B) {
input := []int{5, 2, 8, 1, 9, 3, 7, 4, 6, 0}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringShortSprint(input)
}
}
// Benchmark with medium input (100 elements)
func BenchmarkJoinMedium(b *testing.B) {
input := make([]int, 100)
for i := 0; i < 100; i++ {
input[i] = 100 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringJoin(input)
}
}
func BenchmarkSprintMedium(b *testing.B) {
input := make([]int, 100)
for i := 0; i < 100; i++ {
input[i] = 100 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringSprint(input)
}
}
func BenchmarkShortSprintMedium(b *testing.B) {
input := make([]int, 100)
for i := 0; i < 100; i++ {
input[i] = 100 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringShortSprint(input)
}
}
// Benchmark with large input (1000 elements)
func BenchmarkJoinLarge(b *testing.B) {
input := make([]int, 1000)
for i := 0; i < 1000; i++ {
input[i] = 1000 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringJoin(input)
}
}
func BenchmarkSprintLarge(b *testing.B) {
input := make([]int, 1000)
for i := 0; i < 1000; i++ {
input[i] = 1000 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringSprint(input)
}
}
func BenchmarkShortSprintLarge(b *testing.B) {
input := make([]int, 1000)
for i := 0; i < 1000; i++ {
input[i] = 1000 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringShortSprint(input)
}
}
// Benchmark with very large input (10000 elements)
func BenchmarkJoinVeryLarge(b *testing.B) {
input := make([]int, 10000)
for i := 0; i < 10000; i++ {
input[i] = 10000 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringJoin(input)
}
}
func BenchmarkSprintVeryLarge(b *testing.B) {
input := make([]int, 10000)
for i := 0; i < 10000; i++ {
input[i] = 10000 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringSprint(input)
}
}
func BenchmarkShortSprintVeryLarge(b *testing.B) {
input := make([]int, 10000)
for i := 0; i < 10000; i++ {
input[i] = 10000 - i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringShortSprint(input)
}
}
// Benchmark with empty input
func BenchmarkJoinEmpty(b *testing.B) {
input := []int{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringJoin(input)
}
}
func BenchmarkSprintEmpty(b *testing.B) {
input := []int{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringSprint(input)
}
}
func BenchmarkShortSprintEmpty(b *testing.B) {
input := []int{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = asSortedStringShortSprint(input)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment