问题描述
// code in atoi.go,line 90
var cutoff uint64
switch base {
case 10:
cutoff = maxUint64/10 + 1
case 16:
cutoff = maxUint64/16 + 1
default:
cutoff = maxUint64/uint64(base) + 1
}
我在Golang包的atoi.go文件中看到了一些代码,为什么不像下面这样写?
var cutoff = maxUint64/uint64(base) + 1
非常感谢。
解决方法
我认为您所指的comment above行可能会回答您的问题:
//在常见情况下使用编译时常量。
因为maxUint64/10 + 1
和maxUint64/16 + 1
仅引用常量,编译器才可以计算该常量。结果是,每次调用ParseUint
时都不需要在运行时执行除法运算。您可以在commit中查看基准。