I heard, that there is a way to optimize a * 10 operation (in any language) into something like a * 2 * 2 * 2 + a * 2 and get a great benefit because *2 translates into simple binary shift operation and works much faster than multiplication operation.
Is it right?
I heard, that there is a way to optimize a * 10 operation (in
Share
Yes, that’s true. However, a good compiler may do this automatically for you when multiplying a variable by a suitable constant (if it’s appropriate for the target CPU architecture).
I just tried this with GCC on an Intel target, and
-Odidn’t use the shift-and-add method. I guess theimulinstruction is faster. However, I certainly have seen this type of code generated by GCC with an ARM target, where the multiplication instruction is relatively slow.