In time-critical processes I try avoid introducing branching, but sometimes there is no practical way around it.
In those cases, is there a performance difference between different ways to test for a certain condition? Is it recommended to test for the most likely outcome, or would it make no difference? I.e. is there a different 'weight' between TRUE and FALSE conditions in CPU or compiler architectures?
E.g. will there typically be a difference between the following three snippets:
// snippet 1: test for exception first
if ( !likely_to_happen ) {
foo();
} else {
bar();
}
// snippet 2: test for most common cases first
if ( likely_to_happen ) {
bar();
} else {
foo();
}
// snippet 3: test for exception in an alternative way
if ( not_likely_to_happen) {
foo();
} else {
bar();
}