Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a single "if" slower than "switch"? [duplicate]

Possible Duplicate:
What is the relative performance difference of if/else versus switch statement in Java?

Given the following two methods:

public static int useSwitch(int i) {
    switch (i) {
    case 0:
        return 1;
    default:
        return 0;
    }
}

public static int useIf(int i) {
    if (i == 0)
        return 1;
    return 0;
}

testing shows that the switch executes marginally faster (1.4 nanoseconds per call on my machine) than the if version.

I had always believed that the benefit of a switch didn't kick in until at least a few ifs could be avoided,

Why is switch faster than a single if?

like image 960
Bohemian Avatar asked Dec 30 '12 19:12

Bohemian


People also ask

Why switch case is faster than if-else?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.

Which one is faster if-else or switch?

Speed: A switch statement might prove to be faster than ifs provided number of cases are good. If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too.

Is switch always faster than if?

Generally speaking, switch is always faster than if-else , but isn't always the best solution. Lookup tables are a faster alternative to multiple condition evaluation using if-else or switch .

What are the differences between IF condition and switch?

The if statement evaluates integer, character, pointer or floating-point type or boolean type. On the other hand, switch statement evaluates only character or an integer datatype. Sequence of execution is like either statement under if block will execute or statements under else block statement will execute.


1 Answers

By checking the bytecode the result is as expected:

SWITCH

public static useSwitch(I)I
 L0
  ILOAD 0
  TABLESWITCH
    0: L1
    default: L2
 L1
  INVOKESTATIC Tests.a()I
  IRETURN
 L2
  INVOKESTATIC Tests.b()I
  IRETURN

IF

public static useIf(I)I
 L0
  ILOAD 0
  IFNE L1
 L2
  INVOKESTATIC Tests.a()I
  IRETURN
 L1
  INVOKESTATIC Tests.b()I
  IRETURN

Now I don't see any particular reason for which one should be slower than the other (not by a significative amount in any case). This is surely something that is related to the specific JVM implementation and how it executes these opcodes. According to common knowledge the TABLESWITCH instruction should be slower unless there are enough cases that makes its construction valuable but this is just common thinking. Every JVM could implement it differently so this is just speculation.

Are you sure to profiled everything in a consistent way? (by giving time to JVM to warm up, by keeping just results within a confidence range and all the other things that make profiling enough correct to be used)

like image 181
Jack Avatar answered Sep 22 '22 11:09

Jack