浮点与整数性能

发布于 2024-11-30 15:19:04 字数 161 浏览 1 评论 0原文

开发编程语言时,区分整数和浮点数重要吗?我注意到在 R 的情况下,虽然它们确实允许严格的整数类型,但主要处理可以是浮点数或整数的数字类型。有性能优势吗?

编辑
我也有兴趣了解更多关于何时(如果有的话)人们会通过选择浮点数而不是整数来注意到性能差异。

When developing a programming language, is distinguishing between ints and floats important? I noticed in the case of R that while they do allow for strict integer types, one mainly deals with numeric types which can be floats or ints. Are there performance benefits?

Edit
I'm also interested in learning more about when the time period was (if there was one) where one would notice a difference in performance by choosing a float instead of an integer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2024-12-07 15:19:04

如果您谈论的是性能:对于大多数用途,不存在性能差异。您可能仍然可以在编译为机器代码的纯数字运算代码中进行测量,以及在没有专用 FPU(即主要是嵌入式东西)的硬件上测量数学强度稍低的代码。但对于 Python(和许多其他语言)来说,硬件性能的任何差异都与解释和装箱开销相比相形见绌(许多数量级)。当数字被视为指向 16 字节结构的指针,并且附加是响应解释的操作码的动态分派方法调用时,实际处理花费一纳秒还是一百秒并不重要。

从语义上讲,整数和实数(的近似值)之间的差异仍然并且永远是一个数学事实,而不是计算机工程技术水平的必然结果。例如,浮点数(一般来说,不是从恰好是整数的浮点数进行隐式转换)作为索引永远不会有意义。

If you're talking about performance: For most purposes, there is no performance difference. You can probably still measure one in purely number-crunching code compiled to machine code, and for slightly less math-intense code on hardware that doesn't have a dedicated FPU (i.e. mostly embedded stuff). But for Python (and many other languages), any difference in the hardware's performance is dwarfed (by many many orders of magnitude) by the interpretation and boxing overhead. When number are treated as pointers to 16-byte structures with addition being a dynamically-dispatched method call in response to an interpreted opcode, it doesn't matter if the actual processing takes one nanosecond or hundred.

Semantically, the difference between integers and (approximations of) reals is still and always will be a mathematical fact rather than a necessity that follows from the state of the art of computer engineering. For example, floats (in general, not implicit conversions from floats that are exactly integers) will never make sense as indices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文