Write whatever is easiest for you to read, and compile with optimization.
That being said, in this case, short-circuit evaluation means that your first option is strictly better. Let's see why:
Assume the compiler is very dumb (no optimization). (I've removed your continues, since they also cause a branch, but the idea is the same.)
while(condition){
if(some_condition_1 || some_condition_2){
do_stuff();
}
do_different_stuff();
}
This will have to generate 1 branch instruction: at the if statement, depending on the value of the comparison, the instruction pointer MAY jump past the block containing do_stuff()
and continue
to the point immediately after it (in this case, do_different_stuff()
). Depending on architecture, optimization, and the details of your code, an extra branch may be generated for the short-circuiting behavior of some_condition_1 || some_condition_2
.
What about this version?
while(condition){
if(some_condition_1){
do_stuff();
continue;
}
if(some_condition_2){
do_stuff();
continue;
}
do_different_stuff();
}
Well, first we will hit if(some_condition1)
, and the program will "branch" -- conditionally jumping to if(some_condition2)
. Here, the program will branch again, conditionally jumping to do_different_stuff()
. This is two guaranteed branches! So the first option is better.