Comparing if/else with switch
This example shows how a compiler may be able to optimize a switch statement better than if/else. Below is a sample, but with many more cases, the times are just getting worse. The program doesn't actually do anything useful, but it does demonstrate the difference in performance between the two constructs.The program did 400,000,000 iterations for each value, from 1 to 33, which includes all of the if/else and switch cases and one value that is out of range (for the else or default).
if/else switch int sw4a(int a) { if (a == 1) { return a; } else if (a == 2) { return 7 * a; } else if (a == 3) { return 6 * a; } else if (a == 4) { return 5 * a; } else if (a == 5) { return 4 * a; } else if (a == 6) { return 3 * a; } else if (a == 7) { return 2 * a; } else if (a == 8) { return 3 * a; } else if (a == 9) { return 4 * a; } else if (a == 10) { return 5 * a; } else if (a == 11) { return 6 * a; } else if (a == 12) { return 7 * a; } else if (a == 13) { return 6 * a; } else if (a == 14) { return 5 * a; } else if (a == 15) { return 4 * a; } else if (a == 16) { return 3 * a; } else if (a == 17) { return 3 * a; } else if (a == 18) { return 3 * a; } else if (a == 19) { return 3 * a; } else if (a == 20) { return 3 * a; } else if (a == 21) { return 4 * a; } else if (a == 22) { return 3 * a; } else if (a == 23) { return 5 * a; } else if (a == 24) { return 3 * a; } else if (a == 25) { return 6 * a; } else if (a == 26) { return 3 * a; } else if (a == 27) { return 7 * a; } else if (a == 28) { return 3 * a; } else if (a == 29) { return 8 * a; } else if (a == 30) { return 3 * a; } else if (a == 31) { return 7 * a; } else if (a == 32) { return 3 * a; } else return 5 * a; } int sw4b(int a) { switch (a) { case 1: return 5 * a; break; case 2: return 4 * a; break; case 3: return 3 * a; break; case 4: return 2 * a; break; case 5: return 4 * a; break; case 6: return 5 * a; break; case 7: return 6 * a; break; case 8: return 7 * a; break; case 9: return 8 * a; break; case 10: return 7 * a; break; case 11: return 6 * a; break; case 12: return 5 * a; break; case 13: return 4 * a; break; case 14: return 3 * a; break; case 15: return 2 * a; break; case 16: return 3 * a; break; case 17: return 4 * a; break; case 18: return 3 * a; break; case 19: return 2 * a; break; case 20: return 8 * a; break; case 21: return 3 * a; break; case 22: return 7 * a; break; case 23: return 3 * a; break; case 24: return 6 * a; break; case 25: return 3 * a; break; case 26: return 5 * a; break; case 27: return 3 * a; break; case 28: return 4 * a; break; case 29: return 3 * a; break; case 30: return 7 * a; break; case 31: return 3 * a; break; case 32: return 6 * a; break; default: return 4 * a; } }
Output:
if/else switch Value is 01: Time: 6.902 Value is 02: Time: 7.014 Value is 03: Time: 7.405 Value is 04: Time: 7.560 Value is 05: Time: 7.743 Value is 06: Time: 8.076 Value is 07: Time: 8.209 Value is 08: Time: 8.442 Value is 09: Time: 8.573 Value is 10: Time: 8.858 Value is 11: Time: 9.296 Value is 12: Time: 9.328 Value is 13: Time: 9.631 Value is 14: Time: 10.055 Value is 15: Time: 10.134 Value is 16: Time: 10.397 Value is 17: Time: 10.522 Value is 18: Time: 10.805 Value is 19: Time: 11.376 Value is 20: Time: 11.314 Value is 21: Time: 11.539 Value is 22: Time: 11.947 Value is 23: Time: 11.971 Value is 24: Time: 12.024 Value is 25: Time: 12.242 Value is 26: Time: 12.349 Value is 27: Time: 12.634 Value is 28: Time: 12.797 Value is 29: Time: 13.164 Value is 30: Time: 13.235 Value is 31: Time: 13.676 Value is 32: Time: 13.750 Value is 33: Time: 13.812 Value is 01: Time: 7.100 Value is 02: Time: 6.970 Value is 03: Time: 7.105 Value is 04: Time: 6.995 Value is 05: Time: 6.969 Value is 06: Time: 7.094 Value is 07: Time: 6.972 Value is 08: Time: 7.193 Value is 09: Time: 6.969 Value is 10: Time: 6.961 Value is 11: Time: 6.969 Value is 12: Time: 7.012 Value is 13: Time: 7.071 Value is 14: Time: 6.970 Value is 15: Time: 7.076 Value is 16: Time: 6.974 Value is 17: Time: 7.078 Value is 18: Time: 7.092 Value is 19: Time: 6.980 Value is 20: Time: 6.970 Value is 21: Time: 7.062 Value is 22: Time: 6.968 Value is 23: Time: 7.068 Value is 24: Time: 6.988 Value is 25: Time: 7.067 Value is 26: Time: 7.200 Value is 27: Time: 7.214 Value is 28: Time: 6.974 Value is 29: Time: 7.086 Value is 30: Time: 6.970 Value is 31: Time: 6.961 Value is 32: Time: 7.085 Value is 33: Time: 6.657