Code created while working through "100 Go mistakes and how to avoid them" by Teiva Harsanyi.
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: add no 39

Gabriel (Jun 28, 2024, 7:40 AM +0200) fc7147cf 3708125d

+43
+3
go.mod
··· 1 + module github.com/gm0stache/gomistakes 2 + 3 + go 1.22
+40
no39/main_test.go
··· 1 + package no39_test 2 + 3 + import ( 4 + "strings" 5 + "testing" 6 + ) 7 + 8 + func BenchmarkSliceConcatination(b *testing.B) { 9 + abc := "abcdefghijklmnopqrstuvwxyz" 10 + 11 + testcases := map[string]func(*testing.B){ 12 + "simple concat": func(b *testing.B) { 13 + res := "" 14 + for _, rne := range abc { 15 + res += string(rne) 16 + } 17 + }, 18 + "string builder": func(b *testing.B) { 19 + sb := strings.Builder{} 20 + for _, rne := range abc { 21 + _, _ = sb.WriteRune(rne) 22 + } 23 + _ = sb.String() 24 + }, 25 + "string builder with pre-alloc": func(b *testing.B) { 26 + sb := strings.Builder{} 27 + sb.Grow(len(abc)) 28 + for _, rne := range abc { 29 + _, _ = sb.WriteRune(rne) 30 + } 31 + _ = sb.String() 32 + }, 33 + } 34 + 35 + for range b.N { 36 + for name, bfunc := range testcases { 37 + b.Run(name, bfunc) 38 + } 39 + } 40 + }