golang srpint和itoa对比

在Golang中,将数字转换为字符串,strconv.Itoa的表现比fmt.Sprintf更快。Benchmark测试显示,strconv.Itoa每操作耗时约34.5 ns/op,而fmt.Sprintf耗时约91.3 ns/op。strconv.Itoa内部使用了smallsString和digits进行快速转换,超过特定范围则使用formatBits方法,其中涉及除法和德布鲁因序列等算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 数字转换成字符串,使用 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值