3 ms·
Funny, if you swap the order of the operands in the multiplication, GCC 7.0 with -O3 does optimize it away: int retNum(int num, int num2) { return (n
by anonymouz 10y ago
Funny, if you swap the order of the operands in the multiplication, GCC 7.0 with -O3 does optimize it away:
int retNum(int num, int num2) {
return (num2 * num)/num;
}
now becomes
retNum(int, int):
mov eax, esi
ret
- harpocrates 10y agoIt would be an interesting project to take in some (small) source program, and try all sorts of algebraic rearrangements (maybe just associativity and commutativity of addition and multiplication) to see if there is any noticeable performance change.
- matt_d 10y agoSTOKE, a stochastic superoptimizer, is pretty interesting in this context: http://stoke.stanford.edu/ http://stoke.stanford.edu/ & https://github.com/StanfordPL/stoke https://github.com/StanfordPL/stoke
- dspig 10y agothe performance definitely varies doing this by hand - I guess the current optimizers don't rearrange the code as much as a human can while seeing that it's still equivalent. But sometimes (I'm using LLVM) just rearranging the parentheses to force a different evaluation order makes a difference where I thought it wouldn't have after optimization.
- makapuf 10y agoThis seems to be the case in 6.1 but not in 5.4. What this proves is also that this tool is really useful to check easily between gcc versions without installing all compilers /toolchains