Reputation: 461
Ok, this might sound like a strange question but it is an interesting one. I am coding for iOS and have been told that it is always best to multiply rather than divide values as it is faster.
I know that processors these days probably make this a non issue but my curiosity has gotten the better of me and I am wondering if anyone might be able to shed some light on this for me.
SO..... My question is this -
is:
player.position = ccp(player.contentSize.width / 2, winSize.height / 2);
slower than:
player.position = ccp(player.contentSize.width * 0.5, winSize.height * 0.5);
Upvotes: 6
Views: 1702
Reputation: 93446
On most processors division is slower than multiplication for the same data types. In your example your multiplication is a floating point operation, if width
and height
are integer types, the result may be very different and may depend on both your processor and your compiler.
However most compilers (certainly GCC) will translate a division by a constant power-of-two as in your example, to a right-shift where that would be more efficient. That would generally be faster than either a multiply or divide.
Upvotes: 5
Reputation: 284
Multiplication up-to a certain degree can be done in the parallel, if you can use either use multiplication.
Upvotes: 1
Reputation: 471209
Yes, division is usually much slower than multiplication.
However, when dividing by literals (or anything that can be determined to be a constant at compile-time), the compiler will usually optimize out the division.
Upvotes: 8