Which one is faster -
val = val*10;
or
val = (val<<3) + (val<<2);
How many clock cycles does imul
take when compared to shift instruction?
This is the 21st century. Modern hardware and compilers know how to produce highly optimised code. Writing multiplication using shifts won't help performance but it will help you to produce code with bugs in.
You have demonstrated this yourself with code that multiplies by 12 rather than 10.
I'd say, just write val = val * 10;
or val *= 10;
, and let the compiler worry about such questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With