How to speed up floating-point to integer number conversion?(如何加速浮点到整数的转换?)
问题描述
我们在项目中进行了大量的浮点到整数转换.基本上是这样的
We're doing a great deal of floating-point to integer number conversions in our project. Basically, something like this
for(int i = 0; i < HUGE_NUMBER; i++)
int_array[i] = float_array[i];
执行转换的默认 C 函数非常耗时.
The default C function which performs the conversion turns out to be quite time consuming.
是否有任何解决方法(可能是手动调整功能)可以稍微加快进程?我们不太关心精度.
Is there any work around (maybe a hand tuned function) which can speed up the process a little bit? We don't care much about a precision.
推荐答案
这里的大多数其他答案只是试图消除循环开销.
Most of the other answers here just try to eliminate loop overhead.
只有 deft_code 的答案 触及了真正问题的核心——将浮点数转换为整数是在 x86 处理器上价格高得惊人.deft_code 的解决方案是正确的,尽管他没有给出引用或解释.
Only deft_code's answer gets to the heart of what is likely the real problem -- that converting floating point to integers is shockingly expensive on an x86 processor. deft_code's solution is correct, though he gives no citation or explanation.
这里是技巧的来源,有一些解释以及特定于您是要向上、向下还是向零舍入的版本:了解您的 FPU
Here is the source of the trick, with some explanation and also versions specific to whether you want to round up, down, or toward zero: Know your FPU
很抱歉提供一个链接,但实际上这里写的任何内容,除了复制那篇出色的文章,都不会使事情变得清晰.
Sorry to provide a link, but really anything written here, short of reproducing that excellent article, is not going to make things clear.
这篇关于如何加速浮点到整数的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何加速浮点到整数的转换?
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01