I use else if statements a lot in my programming and not a lot of switch case statements. I know at one point switch case statements become more efficient than else if statements so I was wondering at which point that happens.
Ex: 3 operations, 5 operations, 20 operations.
At what point does a switch case statement become more efficient than repetitive else if statements?
Asked
Active
Viewed 56 times
0

sazzy4o
- 3,073
- 6
- 34
- 62
-
2It largely depends on what you are using for the `case` in the switch: integers, enum values, strings, ...? – hotzst Nov 22 '15 at 19:32
-
3Just to keep this in perspective: we are talking about nanoseconds or at most a couple of microseconds in performance difference. There are few scenarios where this matters as much as code readability. 99.9% of the time there are more relevant ways to improve algorithms. My two cents. – Phil Freihofner Nov 22 '15 at 19:35
-
1It's the point at which `switch` becomes easier to read which really matters. Unless you mean millions of times per second, it is unlikely to matter too much. – Peter Lawrey Nov 22 '15 at 19:39