github.com/iainanderson83/datastructures@v0.0.4-0.20191103204413-889e20b53bcf/arrays/sprintf_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 ) 8 9 func BenchmarkSprintfNumber(b *testing.B) { 10 b.ReportAllocs() 11 vals := make([]string, b.N) 12 for i := 0; i < b.N; i++ { 13 vals[i] = fmt.Sprintf("%d", i) 14 } 15 } 16 17 func BenchmarkSprintfStrconvNumber(b *testing.B) { 18 b.ReportAllocs() 19 vals := make([]string, b.N) 20 for i := 0; i < b.N; i++ { 21 vals[i] = strconv.FormatInt(int64(i), 10) 22 } 23 } 24 25 func BenchmarkBoolSprintf(b *testing.B) { 26 b.ReportAllocs() 27 vals := make([]string, b.N) 28 for i := 0; i < b.N; i++ { 29 vals[i] = fmt.Sprintf("%t", i&1 == 0) 30 } 31 } 32 33 func BenchmarkBoolStrconv(b *testing.B) { 34 b.ReportAllocs() 35 vals := make([]string, b.N) 36 for i := 0; i < b.N; i++ { 37 vals[i] = strconv.FormatBool(i&1 == 0) 38 } 39 } 40 41 func BenchmarkBoolTagSprintfAdd(b *testing.B) { 42 b.ReportAllocs() 43 vals := make([]string, b.N) 44 for i := 0; i < b.N; i++ { 45 vals[i] = "working:" + fmt.Sprintf("%t", i&1 == 0) 46 } 47 } 48 49 func BenchmarkBoolTagSprintf(b *testing.B) { 50 b.ReportAllocs() 51 vals := make([]string, b.N) 52 for i := 0; i < b.N; i++ { 53 vals[i] = fmt.Sprintf("working:%t", i&1 == 0) 54 } 55 } 56 57 func BenchmarkBoolTagIf(b *testing.B) { 58 b.ReportAllocs() 59 vals := make([]string, b.N) 60 for i := 0; i < b.N; i++ { 61 isWorking := i&1 == 0 62 if isWorking { 63 vals[i] = "working:true" 64 } else { 65 vals[i] = "working:false" 66 } 67 } 68 }