C++ 乘法比浮点除法快吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17883240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is multiplication faster than float division?
提问by wodesuck
In C/C++, you can set up the following code:
在 C/C++ 中,您可以设置以下代码:
double a, b, c;
...
c = (a + b) / 2;
This does the exact same thing as:
这与以下内容完全相同:
c = (a + b) * 0.5;
I'm wondering which is better to use. Is one operation fundamentally faster than the other?
我想知道哪个更好用。一个操作从根本上比另一个快吗?
回答by Philip Couling
Multiplication is faster than division. At university I was taught that division takes six times that of multiplication. The actual timings are architecture dependent but in general multiplication will never be slower or even as slow as division. Always optimize your code towards using multiplication if the rounding errors allow.
乘法比除法快。在大学里,我被教导除法是乘法的六倍。实际时间取决于体系结构,但通常乘法永远不会比除法慢,甚至不会像除法一样慢。如果舍入误差允许,请始终优化您的代码以使用乘法。
So in an example this would typically be slower ...
所以在一个例子中,这通常会更慢......
for (int i=0; i<arraySize; i++) {
a[i] = b[i] / x;
}
... than this ...
... 比这个 ...
y=1/x;
for (int i=0; i<arraySize; i++) {
a[i] = b[i] * y;
}
Of course with rounding errors, you'll loose (a little) precision with the second method, but unless you are repeatedly calculating x=1/x;
that's unlikely to cause much issue.
当然,对于舍入误差,第二种方法会降低(一点)精度,但除非您反复计算x=1/x;
,否则不太可能引起太大问题。
Edit:
编辑:
Just for reference. I've dug up a third party comparison of operation timings by searching on Google.
仅供参考。我通过在谷歌上搜索,找到了第三方的操作时间比较。
http://gmplib.org/~tege/x86-timing.pdf
http://gmplib.org/~tege/x86-timing.pdf
Look at the numbers on MUL and DIV. This indicates differences of between 5 and 10 times depending on the processor.
查看 MUL 和 DIV 上的数字。这表示不同处理器之间的差异在 5 到 10 倍之间。
回答by ouah
Floating point multiplication usually takes fewer cycles than floating point division. But with literal operands the optimizer is well aware of this kind of micro-optimizations.
浮点乘法通常比浮点除法花费更少的周期。但是对于文字操作数,优化器很清楚这种微优化。
回答by Mats Petersson
It is quite likely that the compiler will convert a divide to a multiply in this case, if it "thinks" it's faster. Dividing by 2 in floating point may also be faster than other float divides. If the compiler doesn't convert it, it MAY be faster to use multiply, but not certain - depends on the processor itself.
在这种情况下,编译器很可能会将除法转换为乘法,如果它“认为”更快。在浮点中除以 2 也可能比其他浮点除法更快。如果编译器不转换它,使用乘法可能会更快,但不确定 - 取决于处理器本身。
The gain from manually using multiply instead of divide can be quite large in cases where the compiler can't determine that it's "safe" to do so (e.g. 0.1 can't be stored exactly as 0.1 in a floating point number, it becomes 0.10000000149011612). See below for figures on AMD processors which can be taken as representative for the class.
在编译器无法确定这样做“安全”的情况下,手动使用乘法而不是除法的收益可能会非常大(例如,0.1 不能完全存储为浮点数中的 0.1,它变为 0.10000000149011612 )。请参阅下面的 AMD 处理器数据,这些数据可作为该类别的代表。
To tell if your compiler does this well or not, why don't you write a bit of code to experiment. Make sure you write it so that the compiler doesn't just calculate a constant value and discards all the calculation in the loop tho'.
要判断您的编译器是否做得好,为什么不编写一些代码进行实验。确保你编写它以便编译器不只是计算一个常数值并丢弃循环中的所有计算tho'。
Edit:
编辑:
AMD's optimisation guide for Family 15h processors, provide figures for fdiv
and fmul
are 42 and 6 respectively. SSE versions are a little closer, 24 (single) or 27 (double) cycles for DIVPS, DIVPD DIVSS and DIVSD (divide), and 6 cycles for all forms of multiply.
AMD的优化指南家庭15H处理器,用于提供数字fdiv
和fmul
分别为42和6。SSE 版本更接近一些,DIVPS、DIVPD DIVSS 和 DIVSD(除法)为 24(单)或 27(双)周期,所有形式的乘法为 6 个周期。
From memory, Intel's figures aren't that far off.
从记忆中,英特尔的数字并不遥远。