Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency: switch statements over if statements

Tags:

java

pmd

PMD tells me

A switch with less than 3 branches is inefficient, use a if statement instead.

Why is that? Why 3? How do they define efficiency?

like image 900
James Raitsev Avatar asked May 05 '12 03:05

James Raitsev


People also ask

Are switch statements more efficient than if statements?

A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing.

How switch statements are efficient than if-else?

A switch statement works much faster than an equivalent if-else ladder. It's because the compiler generates a jump table for a switch during compilation. As a result, during execution, instead of checking which case is satisfied, it only decides which case has to be executed.

When should you use a switch statement vs an if statement?

An if-else statement can test expression based on a range of values or conditions. A switch statement tests expressions based only on a single integer, enumerated value, or string object. if-else conditional branches are great for variable conditions that result into Boolean.

Why switch statement is better than multiple if statements?

Switch statement works better than multiple if statements when you are giving input directly without any condition checking in the statements. Switch statement works well when you want to increase the readability of the code and many alternative available.


2 Answers

Because a switch statement is compiled with two special JVM instructions that are lookupswitch and tableswitch. They are useful when working with a lot of cases but they cause an overhead when you have just few branches.

An if/else statement instead is compiled into typical je jne ... chains which are faster but require many more comparisons when used in a long chain of branches.

You can see the difference by looking at byte code, in any case I wouldn't worry about these issues, if anything could become a problem then JIT will take care of it.

Practical example:

switch (i) {   case 1: return "Foo";   case 2: return "Baz";   case 3: return "Bar";   default: return null; } 

is compiled into:

L0  LINENUMBER 21 L0  ILOAD 1  TABLESWITCH    1: L1    2: L2    3: L3    default: L4 L1  LINENUMBER 23 L1 FRAME SAME  LDC "Foo"  ARETURN L2  LINENUMBER 24 L2 FRAME SAME  LDC "Baz"  ARETURN L3  LINENUMBER 25 L3 FRAME SAME  LDC "Bar"  ARETURN L4  LINENUMBER 26 L4 FRAME SAME  ACONST_NULL  ARETURN 

While

if (i == 1)   return "Foo"; else if (i == 2)   return "Baz"; else if (i == 3)   return "Bar"; else   return null; 

is compiled into

L0  LINENUMBER 21 L0  ILOAD 1  ICONST_1  IF_ICMPNE L1 L2  LINENUMBER 22 L2  LDC "Foo"  ARETURN L1  LINENUMBER 23 L1 FRAME SAME  ILOAD 1  ICONST_2  IF_ICMPNE L3 L4  LINENUMBER 24 L4  LDC "Baz"  ARETURN L3  LINENUMBER 25 L3 FRAME SAME  ILOAD 1  ICONST_3  IF_ICMPNE L5 L6  LINENUMBER 26 L6  LDC "Bar"  ARETURN L5  LINENUMBER 28 L5 FRAME SAME  ACONST_NULL  ARETURN 
like image 88
Jack Avatar answered Sep 22 '22 12:09

Jack


Although there are minor efficiency gains when using a switch compared to using an if-statement, those gains would be negligible under most circumstances. And any source code scanner worth its salt would recognize that micro-optimizations are secondary to code clarity.

They are saying that an if statement is both simpler to read and takes up fewer lines of code than a switch statement if the switch is significantly short.

From the PMD website:

TooFewBranchesForASwitchStatement: Switch statements are indended to be used to support complex branching behaviour. Using a switch for only a few cases is ill-advised, since switches are not as easy to understand as if-then statements. In these cases use theif-then statement to increase code readability.

like image 45
Tim Pote Avatar answered Sep 23 '22 12:09

Tim Pote