Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which compiles to faster code: "n * 3" or "n+(n*2)"?

Which compiles to faster code: "ans = n * 3" or "ans = n+(n*2)"?

Assuming that n is either an int or a long, and it is is running on a modern Win32 Intel box.

Would this be different if there was some dereferencing involved, that is, which of these would be faster?


long    a;
long    *pn;
long     ans;

...
*pn = some_number;
ans = *pn * 3;

Or

ans = *pn+(*pn*2);

Or, is it something one need not worry about as optimizing compilers are likely to account for this in any case?

like image 441
David L Morris Avatar asked Sep 10 '08 10:09

David L Morris


1 Answers

IMO such micro-optimization is not necessary unless you work with some exotic compiler. I would put readability on the first place.

like image 137
aku Avatar answered Oct 12 '22 23:10

aku