go - 为什么 Golang 像这样在 atoi.go 中处理截止?
问题描述
// 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
非常感谢。
解决方案
我认为您所指的行上方的评论可能会回答您的问题:
// 对常见情况使用编译时常量。
因为只有引用常量maxUint64/10 + 1
,maxUint64/16 + 1
编译器才能计算出来。结果是每次ParseUint
调用都不需要在运行时进行除法运算。您可以在commit中查看基准。
推荐阅读
- kotlin - 我们应该避免在 Kotlin 中使用类型化数组吗?如果是,有没有更新的方法来替换 Kotlin 中的类型化数组?
- python - 类属性未在程序中更新
- angular - 在 Angular 中动态创建 Apollo GraphQL 突变调用
- python - 为什么 Vector2 说它不是浮动的?
- android - 带有 ExoPlayer 和 PlayerControlView 的 MediaBrowserService - 如何从 UI (PlayerControlView) 访问播放器实例?
- excel - 如何在使用 Excel vba 中的“for”循环复制 Excel 表格时添加新幻灯片?
- flutter - textAlign:Textalign.center 不在中心?为什么
- javascript - 页面加载后自动引导模式/对话框不起作用
- javascript - 以下有什么区别吗?
- firebase - 在使用 Firebase + Vue 构建 Web 应用程序时,我们真的需要 Vuex 吗?