Let's say I have
void f(const bool condition) {
if (condition) {
f2();
else {
f3();
}
f4();
if (condition) {
f5();
} else {
f6();
}
}
since condition
never changes, the above can be simplied to the following
void f(const bool condition) {
if (condition) {
f2();
f4();
f5();
} else {
f3();
f4();
f5();
}
}
note that f4()
is duplicated in the second code but second code part has less if
branches. I tried to profile the 2 code snippets but it seems to me that the performance is almost identical. Imagine in real life the above code snippets can have many more if
with the same conditions. So I'm wondering for modern x86/64 processors:
- Is there any performance gain to have 2 giant
if
statements instead of many small ones that are based on the same condition? - Will
const
keyword help compiler/processor generate better branch predictions ?