Does gcc understand or lookahead branches and perform optimization. I wrote a code like this
for (int i = 10;i < 20;i++) {
long long sum = 1;
for (int j = 0;j < 10000;j++) {
sum += pow(i,3);
}
if (i == 15) {
cout<<sum<<" "<<endl;
} else {
cout << 0<<" "<<endl;
}
}
nanos = get_nanos();
printf("current nanos: %ld\n", nanos - last_nanos);
last_nanos = nanos;
If I move the condition if (i == 15) before the for loop, then Ithe execution time reduces by 10x. Why doesnt the compiler understand that the for loop need not be executed unless i == 15?. Is there an option I need to turn on?
I'm compiling with the options - g++ -O3 -lrt