If multiplication is slower than addition instead of doing
7 * 8
Will this theoretically improve performance ?
for(int i =0; i < 8 ; i++){
temp += 7
}
Or else do i just need to do
7 + 7 + 7 + 7 + 7 + 7 + 7 + 7
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Have you tried it and timed it?
On nearly every single modern machine today, there is fast hardware support for multiplication. So unless it’s simple multiplication by 2, no, it will not be faster.
To give some hard numbers on the current Intel machines:
Taken from Agner Fog’s manuals.
Although it’s actually a lot more complicated than this, the point is still: No, you’re not going to get any benefit trying to replace multiplications with additions.
In the few cases where it is better (such as multiplication by a power of two – using shifts), the compiler will make that optimization for you, if it knows the value at compile-time.
On x86, the compiler can also play with the
leainstruction to do fast multiplication by 3, 5, 10, etc…