If vs Switch Performance, which one is better? (only one way to find out) FIGHT, FIGHT, FIGHT!
codingninja.co.uk/if-vs-switch-performance-to...Posted Mar 24, 2014, 9:15 AM UTC
03
Posted Mar 24, 2014, 9:15 AM UTC
Also, you should use else if() for every subsequent if statement, so that it won't have to evaluate every expression if the first is true (you could also use goto, as you essentially don't care about the rest of the if-statements)
[deleted]
The author is only micro benchmarking an if-vs-switch with a couple of options. I imagine the positive results for the "if" alternative has more to do with data locality and CPU cache. It'd be really interesting to see a comparison with different amount of elements. Ie, 10, 100, 1000, 10000, 100000 elements.
I would be interested to see him do the comparison again using integers instead of strings. Since he is using 2 character strings, you should be able to typecast them to a short or long integer and uses those in the switch instead. It will eliminate all the expensive string comparison operations.