Compiler optimization causing program to run slower

后端 未结 3 1569
孤街浪徒
孤街浪徒 2020-12-11 04:00

I have the following piece of code that I wrote in C. Its fairly simple as it just right bit-shifts x for every loop of for.

int main() {
   int         


        
3条回答
  •  既然无缘
    2020-12-11 04:12

    You will get the definitive answer by looking at the binary that's produced (using objdump or something).

    But as others have noted, this is probably because you're relying on undefined behaviour. One possible explanation is that the compiler is free to assume that i will never be less than -2, and so will eliminate the conditional entirely, and convert this into an infinite loop.

    Also, your code has no observable side effects, so the compiler is also free to optimise the entire program away to nothing, if it likes.

提交回复
热议问题