EDIT:
I did some research and I have some nice results. How would you explain this behavior ?
Sorry for my latest edit, but I will first show case 1 and case 2 times. As you canhad some cache problems as far as I could see case 2 runs faster, at least on my machine.
alin@ubuntu:~/Desktop$ time ./1
real 0m4.025s
user 0m4.008s
sys 0m0.020s
alin@ubuntu:~/Desktop$ time ./2
real 0m3.285s
user 0m3.272s
sys 0m0these are more accurate results and code samples, I hope.016s
#include <stdio.h> extern int * cache; extern bool * b; extern int * x; extern int * a; extern unsigned long * loop; extern void A(); extern void B(); int main() { for (unsigned long i = 0; i < *loop; ++i) { ++*cache; *x += *a; if (*b) { A(); } else { B(); } } delete b; delete x; delete a; delete loop; delete cache; return 0; } int * cache = new int(0); bool * b = new bool(true); int * x = new int(0); int * a = new int(0); unsigned long * loop = new unsigned long(0xfffffffe0x0ffffffe); void A() { --*x; *b = false; } void B() { ++*x; *b = true; } #include <stdio.h> extern int * cache; extern bool * b; extern int * x; extern int * a; extern unsigned long * loop; extern void A(); extern void B(); int main() { for (unsigned long i = 0; i < *loop; ++i) { ++*cache; if (*b) { *x += *a; A(); } else { *x += *a; B(); } } delete b; delete x; delete a; delete loop; delete cache; return 0; } int * cache = new int(0); bool * b = new bool(true); int * x = new int(0); int * a = new int(0); unsigned long * loop = new unsigned long(0xfffffffe0x0ffffffe); void A() { --*x; *b = false; } void B() { ++*x; *b = true; } So thisThere is pretty much says what I believed ? Ifunnoticeable difference between the compiler has no way to know at compile time-O3 versions of both approaches, then he can't optimize thatbut without -O3, therefore you should do itsecond case does run slightly faster, at least on my machine. I have tested without ?-O3 and with the loop = 0xfffffffe.
Best times:
alin@ubuntu:~/Desktop$ time ./1
real 0m20.231s
user 0m20.224s
sys 0m0.020s
alin@ubuntu:~/Desktop$ time ./2
real 0m19.932s
user 0m19.890s
sys 0m0.060s