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

后端 未结 8 1206
予麋鹿
予麋鹿 2020-11-28 05:52

Which is the better and fastest methods : if or switch ?

if(x==1){
  echo \"hi\";
} else if (x==2){
  echo \"bye\";
}

switch(x){
  case 1
    ...
  break;
          


        
相关标签:
8条回答
  • 2020-11-28 06:05

    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.

    0 讨论(0)
  • 2020-11-28 06:09

    General rule is use switch whenever the number of conditions is greater than 3 (for readability).

    if / else if / else is more flexible (hence better), but switch is slightly faster because it just computes the condition once and then checks for the output, while if has to do this every time.

    EDIT: Seems like switch is slower than if after all, I could swear this was not the case...

    0 讨论(0)
  • 2020-11-28 06:12

    I belive the compiler will turn them into very similar, or maybe even identical code at the end of the day.

    Unless you're doing something weird, don't try and do the optimisation for the compiler.

    Also, developer time is generally more important than runtime (with the exception of games), so it'sbbetter to make its more readable and maintainable.

    0 讨论(0)
  • 2020-11-28 06:23

    When using ==, performance of if ... elseif compared to switch is almost identically. However, when using ===, if ... elseif is about 3 times faster (according to: phpbench).

    Generally, you should go with what is most readable and use switch when making more than 3 comparisons. If performance is a major concern and you don't need to make any type conversions, then use if ... elseif with ===.

    0 讨论(0)
  • 2020-11-28 06:26

    in my opinion the "if/else" is faster but not better than switch but i prefer this:

    echo ($x==1?"hi":($x==2?"bye":""));
    

    if you have to do 1,2 cases like if/else if/else

    0 讨论(0)
  • 2020-11-28 06:27

    I found this post: https://gist.github.com/Jeff-Russ/2105d1a9e97a099ca1509de1392cd314 which indicates switch/case to be faster than if/elseif with ===.

    They also indicate nested if statements which makes a lot more sense and also provide far better results.

    Their times:

    nested if/elseif === : 0.25623297691345 (NESTED IF)

    switch/case : 0.33157801628113 (SWITCH CASE)

    if/elseif with === : 0.45587396621704 (FLAT IF)

    only if with === : 0.45587396621704 (ONLY IF)

    0 讨论(0)
提交回复
热议问题