2

Possible Duplicate:
Why is float division slow?

I heard that a computer does the operation .5*x faster than x/2. Is this true? Can you please tell me why or how this works?

Community
  • 1
  • 1
user13267
  • 6,871
  • 28
  • 80
  • 138
  • No idea if any compiler/platform can do this, but dividing/multiplying a float/double by `2` just means subtracting/adding `1` from the exponent. Haven't thought thru whether corner cases complicate this. – Keith Sep 15 '11 at 03:55
  • I've heard this too, I know it has to do with the way the hardware processes the data, but I don't know enough about it to give a decent explanation. However, this page seems to have some good answers that may be useful to you. http://stackoverflow.com/questions/506237/why-is-float-division-slow – Andrew Black Sep 15 '11 at 03:23
  • As a hint, consider the manual process of multiplication vs the manual process of division. One consists of a sequence of direct computation, while the other involves a bunch of computation and decision making along the way. – phkahler Sep 15 '11 at 15:57

2 Answers2

1

That is not generally speaking true. It will depend on the instruction set of the microprocessor. Sometimes there is no native '/' operation, so the compiler will use two: clocks one to get the .5 and one to multiply, while its cousin .5*x will just use one.

But there are no restriction on having '/', there may be a microprocessors that do have a native '/', so it will depend.

fceruti
  • 2,405
  • 1
  • 19
  • 28
1

Short answer: Yes, the multiplication is usually faster.

For particular cases it could depend on many things e.g. the platform, language, compiler, hardware, presence or absence of lookup tables etc. For integer division by powers of 2, bit shifting is sometimes a little faster again. But compilers can usually optimise those cases.

wim@wim-acer:~/Desktop$ python -mtimeit '0.5*1234567890.'
100000000 loops, best of 3: 0.0168 usec per loop
wim@wim-acer:~/Desktop$ python -mtimeit '1234567890./2.'
10000000 loops, best of 3: 0.043 usec per loop
wim@wim-acer:~/Desktop$ python -mtimeit '1234567890 >> 1'
100000000 loops, best of 3: 0.0168 usec per loop

When writing C++ code, when I have to do division by a constant k from inside a loop, I have often been able to squeeze out some gains in performance-critical code by defining double ki = 1./k outside the loop and using multiplication by ki inside the loop.

wim
  • 338,267
  • 99
  • 616
  • 750