1

Possible Duplicate:
Should I use multiplication or division?
Compare multiplication and division

Is division more expensive than multiplication in C++?

I am doing optimization and I can turn B/alpha_B into B*alpha_A, for some comparison reasons. I can choose now should I go with multiplication or division?

Community
  • 1
  • 1
user1509260
  • 3,353
  • 3
  • 18
  • 9
  • 4
    what has **your** profiling shown you? –  Jul 14 '12 at 05:46
  • 2
    you implement both, measure and see which is faster; *here is a hint, it probably isn't measurable and most likely doesn't really matter, it isn't 1983 anymore!* –  Jul 14 '12 at 05:52

2 Answers2

10

This has nothing to do with C++ (or any other language) and everything to do with the underlying processor architecture/implementation. Division is generally on the order of 10x slower than multiplication on most processor families.

That said, the latency of a divide instruction is rarely the main performance hotspot in a program these days (your mileage may vary, of course). The place where it makes most sense to replace division with multiplication by the reciprocal is when you're dividing a vector by a constant factor (e.g. vector normalization, scaling, matrix pivoting). This is also the place where pipeline depth will do the most to mask the div instruction latency and memory bandwidth (assuming large vectors) will likely become the throughput limitation.

In short, it's rarely worthwhile to engage in these types of micro-optimizations these days. Most likely the compiler optimizer will recognize cases where it makes sense to substitute multiplication for division anyways.

Drew Hall
  • 28,429
  • 12
  • 61
  • 81
0

I found a source claiming that multiply is faster:

http://simpletonprogrammer.blogspot.com/2012/01/assumed-optimization-floating-point.html

The timings for FMUL & FDIV seem to be fairly conclusive, at least for the processors listed. Other processors may be different, however. In other words, learn to profile.

Qsario
  • 1,016
  • 1
  • 9
  • 18