Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a shift instruction faster than an IMUL instruction?

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?

like image 861
Kartlee Avatar asked May 25 '11 06:05

Kartlee


2 Answers

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.

like image 161
David Heffernan Avatar answered Sep 21 '22 04:09

David Heffernan


I'd say, just write val = val * 10; or val *= 10;, and let the compiler worry about such questions.

like image 32
Henno Brandsma Avatar answered Sep 19 '22 04:09

Henno Brandsma