浮点与整数性能
开发编程语言时,区分整数和浮点数重要吗?我注意到在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您谈论的是性能:对于大多数用途,不存在性能差异。您可能仍然可以在编译为机器代码的纯数字运算代码中进行测量,以及在没有专用 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.