Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If vs Case statements [closed]

Are there any performance differences between using if-else and case statements when handling multiple conditions?

Which is preferred?

like image 247
fealin Avatar asked Dec 22 '09 14:12

fealin


2 Answers

In some languages, like C, switch may possibly be faster because it's usually implemented with a jump table. Modern compilers sometimes are smart enough to use one for several ifs as well, though.

And anyway it probably won't matter, micro optimizations are (almost) never useful.

like image 141
Thomas Bonini Avatar answered Sep 20 '22 18:09

Thomas Bonini


Use the one that's most readable in the given context.

like image 20
Alex Brasetvik Avatar answered Sep 20 '22 18:09

Alex Brasetvik