Performance shouldn't be your primary concern for the first decision of how to write your code. You should never do performance tweaks before there is a reason to, meaning, you first write your program in a well-readable manner, afterwards you check for bottlenecks with a profiler. Only then you do performance tweaks.
Otherwise you end up tweaking code that does not even run often enough to be important for the performance of your program.
And more importantly: Performance tweaks might introduce bugs, because very often they end up in less-readable code.
People who care about the performance of
every switch-case/if-else statement in their code, should rather switch to writing assembly.
The deciding factor is for me readability and complexity.
If-else statements allow more complex conditional statements, some of them can not be realized with a switch-case, so the decision is clear for these.
Switch-case statements tend to be more readable. Just an example in Java. This is the if-else latter:
Code:
if(key == COFFHeaderKey.CHARACTERISTICS) {
b.append(NL + description + ": " + NL);
b.append(IOUtil.getCharacteristics(value, "characteristics"));
} else if (key == COFFHeaderKey.TIME_DATE) {
b.append(description + ": ");
b.append(convertToDate(value));
} else if (key == COFFHeaderKey.MACHINE) {
b.append(description + ": ");
b.append(getMachineTypeString((int) value));
} else {
b.append(field.toString());
}
And this is it after refactoring to use a switch-case statement:
Code:
switch (key) {
case CHARACTERISTICS:
b.append(NL + description + ": " + NL);
b.append(IOUtil.getCharacteristics(value, "characteristics"));
break;
case TIME_DATE:
b.append(description + ": ");
b.append(convertToDate(value));
break;
case MACHINE:
b.append(description + ": ");
b.append(getMachineTypeString((int) value));
break;
default:
b.append(field.toString());
}