Should I use multiplication or division?

2019-01-02 20:36发布

Here's a silly fun question:

Let's say we have to perform a simple operation where we need half of the value of a variable. There are typically two ways of doing this:

y = x / 2.0;
// or...
y = x * 0.5;

Assuming we're using the standard operators provided with the language, which one has better performance?

I'm guessing multiplication is typically better so I try to stick to that when I code, but I would like to confirm this.

Although personally I'm interested in the answer for Python 2.4-2.5, feel free to also post an answer for other languages! And if you'd like, feel free to post other fancier ways (like using bitwise shift operators) as well.

25条回答
查无此人
2楼-- · 2019-01-02 21:05

Floating-point division is (generally) especially slow, so while floating-point multiplication is also relatively slow, it's probably faster than floating-point division.

But I'm more inclined to answer "it doesn't really matter", unless profiling has shown that division is a bit bottleneck vs. multiplication. I'm guessing, though, that the choice of multiplication vs. division isn't going to have a big performance impact in your application.

查看更多
千与千寻千般痛.
3楼-- · 2019-01-02 21:05

There is a difference, but it is compiler dependent. At first on vs2003 (c++) I got no significant difference for double types (64 bit floating point). However running the tests again on vs2010, I detected a huge difference, up to factor 4 faster for multiplications. Tracking this down, it seems that vs2003 and vs2010 generates different fpu code.

On a Pentium 4, 2.8 GHz, vs2003:

  • Multiplication: 8.09
  • Division: 7.97

On a Xeon W3530, vs2003:

  • Multiplication: 4.68
  • Division: 4.64

On a Xeon W3530, vs2010:

  • Multiplication: 5.33
  • Division: 21.05

It seems that on vs2003 a division in a loop (so the divisor was used multiple times) was translated to a multiplication with the inverse. On vs2010 this optimization is not applied any more (I suppose because there is slightly different result between the two methods). Note also that the cpu performs divisions faster as soon as your numerator is 0.0. I do not know the precise algorithm hardwired in the chip, but maybe it is number dependent.

Edit 18-03-2013: the observation for vs2010

查看更多
大哥的爱人
4楼-- · 2019-01-02 21:05

Well, if we assume that an add/subtrack operation costs 1, then multiply costs 5, and divide costs about 20.

查看更多
忆尘夕之涩
5楼-- · 2019-01-02 21:05

Here's a silly fun answer:

x / 2.0 is not equivalent to x * 0.5

Let's say you wrote this method on Oct 22, 2008.

double half(double x) => x / 2.0;

Now, 10 years later you learn that you can optimize this piece of code. The method is referenced in hundreds of formulas throughout your application. So you change it, and experience a remarkable 5% performance improvement.

double half(double x) => x * 0.5;

Was it the right decision to change the code? In maths, the two expressions are indeed equivalent. In computer science, that does not always hold true. Please read Minimizing the effect of accuracy problems for more details. If your calculated values are - at some point - compared with other values, you will change the outcome of edge cases. E.g.:

double quantize(double x)
{
    if (half(x) > threshold))
        return 1;
    else
        return -1;
}

Bottom line is; once you settle for either of the two, then stick to it!

查看更多
心情的温度
6楼-- · 2019-01-02 21:06

I have always learned that multiplication is more efficient.

查看更多
琉璃瓶的回忆
7楼-- · 2019-01-02 21:07

This becomes more of a question when you are programming in assembly or perhaps C. I figure that with most modern languages that optimization such as this is being done for me.

查看更多
登录 后发表回答