- 数字转换成字符串,使用 strconv.Itoa() 比 fmt.Sprintf() 要快
func BenchmarkItoa(b *testing.B) {
number := 1234567890
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.Itoa(number)
}
}
func BenchmarkSprint(b *testing.B) {
number := 1234567890
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Sprint(number)
}
}
结果
goos: windows
goarch: amd64
pkg: test/one
BenchmarkItoa
BenchmarkItoa-12 33422924 34.5 ns/op
BenchmarkSprint
BenchmarkSprint-12 13076952 91.3 ns/op
对比一下源码
func FormatInt(i int64, base int) string {
if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
return small(int(i))
}
_, s := formatBits(nil, uint64(i), base, i < 0, false)
return s
}
func small(i int) string {
if i < 10 {
return digits[i : i+1]
}
return smallsString[i*2 : i*2+2]
}
Itoa核心调用就是small和formatBits,10以内直接用digits这个代表36进制的字符串转换,100以内直接用smallsString字符串转换,超过100的调用formatBits转换
b := uint64(base)
for u >= b {
i--
// Avoid using r = a%b in addition to q = a/b
// since 64bit division and modulo operations
// are calculated by runtime functions on 32bit machines.
q := u / b
a[i] = digits[uint(u-q*b)]
u = q
}
// u < base
i--
a[i] = digits[uint(u)]
formatBits核心代码就是这一块,对2的倍数和10进制做了特殊处理。
for us >= 100 {
is := us % 100 * 2
us /= 100
i -= 2
a[i+1] = smallsString[is+1]
a[i+0] = smallsString[is+0]
}
10进制核心代码就是这一块
// Avoid using r = a%b in addition to q = a/b
// since 64bit division and modulo operations
// are calculated by runtime functions on 32bit machines.
q := u / 1e9
us := uint(u - q*1e9) // u % 1e9 fits into a uint
这里有两句比较有意思的,uint64用了除法再相减而不是直接取余
2倍数进制的使用了德布鲁因序列,剩下的就是按进制除法
func Sprint(a ...interface{}) string {
p := newPrinter()
p.doPrint(a)
s := string(p.buf)
p.free()
return s
}
for u >= 10 {
i--
next := u / 10
buf[i] = byte('0' + u - next*10)
u = next
}
以10进制的有符号整数为例,核心代码就是以相除写进buf