Which Is Faster of Two Case or If

Is else if faster than switch() case?

For just a few items, the difference is small. If you have many items you should definitely use a switch.

If a switch contains more than five items, it's implemented using a lookup table or a hash list. This means that all items get the same access time, compared to a list of if:s where the last item takes much more time to reach as it has to evaluate every previous condition first.

Which is faster - if..else or Select..case?

If you compile the two fragments and use reflector to disassemble you will see that they both end up as the practically the same IL. The compiler replaces the if / else with case statement.

This kind of micro optimization is highly unlikely to help you if you have performance problems.

If you have performance problems then you need to profile the program and find out where the bottlenecks are.

If you don't have performance problems, stop sweating this stuff and worry about writing code that is easily understood.

Case vs If Else If: Which is more efficient?

It seems that the compiler is better in optimizing a switch-statement than an if-statement.

The compiler doesn't know if the order of evaluating the if-statements is important to you, and can't perform any optimizations there. You could be calling methods in the if-statements, influencing variables. With the switch-statement it knows that all clauses can be evaluated at the same time and can put them in whatever order is most efficient.

Here's a small comparison:

http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx

Which is Faster and better, Switch Case or if else if?

Your first example is simply wrong. You need elseif instead of just else.

If you use if..elseif... or switch is mainly a matter of preference. The performance is the same.

However, if all your conditions are of the type x == value with x being the same in every condition, switch usually makes sense. I'd also only use switch if there are more than e.g. two conditions.

A case where switch actually gives you a performance advantage is if the variable part is a function call:

switch(some_func()) {
case 1: ... break;
case 2: ... break;
}

Then some_func() is only called once while with

if(some_func() == 1) {}
elseif(some_func() == 2) {}

it would be called twice - including possible side-effects of the function call happening twice. However, you could always use $res = some_func(); and then use $res in your if conditions - so you can avoid this problem alltogether.

A case where you cannot use switch at all is when you have more complex conditions - switch only works for x == y with y being a constant value.

If vs. Switch Speed

The compiler can build jump tables where applicable. For example, when you use the reflector to look at the code produced, you will see that for huge switches on strings, the compiler will actually generate code that uses a hash table to dispatch these. The hash table uses the strings as keys and delegates to the case codes as values.

This has asymptotic better runtime than lots of chained if tests and is actually faster even for relatively few strings.

Java switch statement with two cases or if statement?

The answer is really depends on the interest of usage. What usually I do is, If my conditions are more (3 or above) I'l go for switch. Otherwise I simply use if-else-if.

Java switch statement with two cases or if statement?

For your case the first is good enough which is if else. If there are more cases like that, prefer switch. That increases readability versus ugly good amount of if else's.

Is 'switch' faster than 'if'?

There are several optimizations a compiler can make on a switch. I don't think the oft-mentioned "jump-table" is a very useful one though, as it only works when the input can be bounded some way.

C Pseudocode for a "jump table" would be something like this -- note that the compiler in practice would need to insert some form of if test around the table to ensure that the input was valid in the table. Note also that it only works in the specific case that the input is a run of consecutive numbers.

If the number of branches in a switch is extremely large, a compiler can do things like using binary search on the values of the switch, which (in my mind) would be a much more useful optimization, as it does significantly increase performance in some scenarios, is as general as a switch is, and does not result in greater generated code size. But to see that, your test code would need a LOT more branches to see any difference.

To answer your specific questions:

  1. Clang generates one that looks like this:

    test_switch(char):                       # @test_switch(char)
    movl %edi, %eax
    cmpl $19, %edi
    jbe .LBB0_1
    retq
    .LBB0_1:
    jmpq *.LJTI0_0(,%rax,8)
    jmp void call<0u>() # TAILCALL
    jmp void call<1u>() # TAILCALL
    jmp void call<2u>() # TAILCALL
    jmp void call<3u>() # TAILCALL
    jmp void call<4u>() # TAILCALL
    jmp void call<5u>() # TAILCALL
    jmp void call<6u>() # TAILCALL
    jmp void call<7u>() # TAILCALL
    jmp void call<8u>() # TAILCALL
    jmp void call<9u>() # TAILCALL
    jmp void call<10u>() # TAILCALL
    jmp void call<11u>() # TAILCALL
    jmp void call<12u>() # TAILCALL
    jmp void call<13u>() # TAILCALL
    jmp void call<14u>() # TAILCALL
    jmp void call<15u>() # TAILCALL
    jmp void call<16u>() # TAILCALL
    jmp void call<17u>() # TAILCALL
    jmp void call<18u>() # TAILCALL
    jmp void call<19u>() # TAILCALL
    .LJTI0_0:
    .quad .LBB0_2
    .quad .LBB0_3
    .quad .LBB0_4
    .quad .LBB0_5
    .quad .LBB0_6
    .quad .LBB0_7
    .quad .LBB0_8
    .quad .LBB0_9
    .quad .LBB0_10
    .quad .LBB0_11
    .quad .LBB0_12
    .quad .LBB0_13
    .quad .LBB0_14
    .quad .LBB0_15
    .quad .LBB0_16
    .quad .LBB0_17
    .quad .LBB0_18
    .quad .LBB0_19
    .quad .LBB0_20
    .quad .LBB0_21
  2. I can say that it is not using a jump table -- 4 comparison instructions are clearly visible:

    13FE81C51 cmp  qword ptr [rsp+30h],1 
    13FE81C57 je testSwitch+73h (13FE81C73h)
    13FE81C59 cmp qword ptr [rsp+30h],2
    13FE81C5F je testSwitch+87h (13FE81C87h)
    13FE81C61 cmp qword ptr [rsp+30h],3
    13FE81C67 je testSwitch+9Bh (13FE81C9Bh)
    13FE81C69 cmp qword ptr [rsp+30h],4
    13FE81C6F je testSwitch+0AFh (13FE81CAFh)

    A jump table based solution does not use comparison at all.

  3. Either not enough branches to cause the compiler to generate a jump table, or your compiler simply doesn't generate them. I'm not sure which.

EDIT 2014: There has been some discussion elsewhere from people familiar with the LLVM optimizer saying that the jump table optimization can be important in many scenarios; e.g. in cases where there is an enumeration with many values and many cases against values in said enumeration. That said, I stand by what I said above in 2011 -- too often I see people thinking "if I make it a switch, it'll be the same time no matter how many cases I have" -- and that's completely false. Even with a jump table you get the indirect jump cost and you pay for entries in the table for each case; and memory bandwidth is a Big Deal on modern hardware.

Write code for readability. Any compiler worth its salt is going to see an if / else if ladder and transform it into equivalent switch or vice versa if it would be faster to do so.



Related Topics



Leave a reply



Submit