I have a switch
statement in some time-critical code. I was trying to optimize it with __builtin_expect
, but it does not seem to be working. I'm wondering if anyone can tell me if I'm missing some detail, or if the compiler simply does not optimize for it. I have tried the following on my host machine:
int main() {
volatile int v=0;
long i = 0;
for (i=0; i<1000000000L; i++) {
switch(__builtin_expect(v, EXPT)) {
case 7:
v=7;
break;
default:
v=7;
break;
}
}
return v;
}
Then I compile and run as follows:
~/code/builtinexpect> gcc bie.c -o bie -D EXPT=0 && time ./bie
real 0m2.092s
user 0m2.086s
sys 0m0.000s
~/code/builtinexpect> gcc bie.c -o bie -D EXPT=7 && time ./bie
real 0m2.092s
user 0m2.086s
sys 0m0.000s
I am using GCC version 4.5.1.