division / multiplication efficiency

Say I have functions like so: code: -------- circleRadians = 2 * pi radius = (/ circleRadians) -------- My question: each time I call radius, will a division operation be performed? Is there anything to be gained, following the old adage that multiplication is more efficient than division in hardware, from something like: code: -------- radius = (* (1 / circleRadians)) -------- ...imagining that the compiler (or runtime graph reduction mechanism) will transform (1 / circleRadians) into a single number (s) only once, and use (* s) in all applications of radius? -- frigidcode.com

On Wed, Nov 28, 2012 at 04:02:38AM -0900, Christopher Howard wrote:
Say I have functions like so:
code: -------- circleRadians = 2 * pi
radius = (/ circleRadians) --------
My question: each time I call radius, will a division operation be performed?
Probably yes.
Is there anything to be gained, following the old adage that multiplication is more efficient than division in hardware, from something like:
code: -------- radius = (* (1 / circleRadians)) --------
If this actually makes a difference (and isn't dwarfed by other considerations), then you have some very fiddly, low-level, tightly optimized code indeed. And modern processors are so fancy and complex, the real answer is probably "it depends", anyway. If you really think it matters, then profile an actual program to see the difference (using e.g. the 'criterion' package). But the REAL answer is, "you have better things to do with your time than worry about this". -Brent

This may help on the multiplication versus division question: http://www.cplusplus.com/forum/general/17811/ -- -- Regards, KC

On 11/28/2012 08:02 AM, Christopher Howard wrote:
Say I have functions like so:
code: -------- circleRadians = 2 * pi
radius = (/ circleRadians) --------
My question: each time I call radius, will a division operation be performed? Is there anything to be gained, following the old adage that multiplication is more efficient than division in hardware, from something like:
This doesn't answer your question, but is an interesting read: http://stackoverflow.com/questions/12653787/what-optimizations-can-ghc-be-ex... I've seen a lot of surprising stuff come out of the optimizer. If you really want to know which is faster, try both ways.
participants (4)
-
Brent Yorgey
-
Christopher Howard
-
KC
-
Michael Orlitzky