strconv 性能优于 fmt

将原语转换为字符串或从字符串转换时,strconv 速度比 fmt 更快。

BadGood
  1. for i := 0; i < b.N; i++ {
  2. s := fmt.Sprint(rand.Int())
  3. }
  1. for i := 0; i < b.N; i++ {
  2. s := strconv.Itoa(rand.Int())
  3. }
  1. BenchmarkFmtSprint-4 143 ns/op 2 allocs/op
  1. BenchmarkStrconv-4 64.2 ns/op 1 allocs/op