From what I have read I can summarize,
- Switch case is implementation defined but is mostly defined as a jump table
- Switch case makes the code more readable
- Switch is faster than
if/elseif
(?)
Consider a case where I have 300+ switch cases. I know an if/elseif
in this scene will be a mess.
But I want to know how will a switch
case perform in
such a scene?
- Is it scalable i.e it remains relatively faster than an if/else no matter how many cases are present ?
- Since it is implementation defined how can I figure out how my compiler is implementing it?
- And above all how do I do this
if/elseif
-switch
comparison apart from actually writing the code and using a profiler? I have tried compiling a small.c
file with switch case usinggcc 4.8.1
-S
switch and it looks like a jump table is created.Where do I go from here? - Is it better/worse to use an
if/elseif
in such scenarios
I am primarily interested in C/C++ specific details