在基础类型和string类型相互转换的时候,strconv
比 fmt 快。
Bad
for i := 0; i < b.N; i++ {
s := fmt.Sprint(rand.Int())
}
// BenchmarkFmtSprint-4 143 ns/op 2 allocs/op
Good
for i := 0; i < b.N; i++ {
s := strconv.Itoa(rand.Int())
}
// BenchmarkStrconv-4 64.2 ns/op 1 allocs/op