Is there any significant difference between using if/else and switch-case in C#?

前端 未结 20 796
时光取名叫无心
时光取名叫无心 2020-11-22 07:25

What is the benefit/downside to using a switch statement vs. an if/else in C#. I can\'t imagine there being that big of a difference, other than m

相关标签:
20条回答
  • 2020-11-22 07:40

    As per this link, IF vs Switch comparison of iteration test using switch and if statement, is like for 1,000,000,000 iterations, Time taken by Switch Statement=43.0s & by If Statement = 48.0s

    Which is literally 20833333 iterations per second, So, Should we really need to focus more,

    P.S:Just to know the performance difference for small list of conditions.

    0 讨论(0)
  • 2020-11-22 07:41

    My 2 cents on it. Well most of the times if performance is not a criteria than it's more about code readability. If the the number of if/else statements are too many than using switch statement is better.

    0 讨论(0)
  • 2020-11-22 07:44

    SWITCH statement only produces same assembly as IFs in debug or compatibility mode. In release, it will be compiled into jump table (through MSIL 'switch' statement)- which is O(1).

    C# (unlike many other languages) also allows to switch on string constants - and this works a bit differently. It's obviously not practical to build jump tables for strings of arbitrary lengths, so most often such switch will be compiled into stack of IFs.

    But if number of conditions is big enough to cover overheads, C# compiler will create a HashTable object, populate it with string constants and make a lookup on that table followed by jump. Hashtable lookup is not strictly O(1) and has noticeable constant costs, but if number of case labels is large, it will be significantly faster than comparing to each string constant in IFs.

    To sum it up, if number of conditions is more than 5 or so, prefer SWITCH over IF, otherwise use whatever looks better.

    0 讨论(0)
  • 2020-11-22 07:46

    Not just C#, but all C-based languages, I think: because a switch is limited to constants, it's possible to generate very efficient code using a "jump table". The C case is really a good old FORTRAN computed GOTO, but the C# case is still tests against a constant.

    It is not the case that the optimizer will be able to make the same code. Consider, eg,

    if(a == 3){ //...
    } else if (a == 5 || a == 7){ //...
    } else {//...
    }
    

    Because those are compound booleans, the generated code has to compute a value, and shortcircuit. Now consider the equivalent

    switch(a){
       case 3: // ...
        break;
       case 5:
       case 7: //...
        break;
       default: //...
    }
    

    This can be compiled into

    BTABL: *
    B3:   addr of 3 code
    B5:
    B7:   addr of 5,7 code
          load 0,1 ino reg X based on value
          jump indirect through BTABL+x
    

    because you are implicitly telling the compiler that it doesn't need to compute the OR and equality tests.

    0 讨论(0)
  • 2020-11-22 07:48

    a switch statement basicly is a comparison for equality. keyboard event's have a great advantage over switch statement's when having easy to write and read code then an if elseif statement would, missing a {bracket} could get troubling as well.

    char abc;
    switch(abc)
    {
    case a: break;
    case b: break;
    case c: break;
    case d: break;
    }
    

    An if elseif statement is great for more then one solution if(theAmountOfApples is greater then 5 && theAmountOfApples is less then 10) save your apples else if(theAmountOfApples is greater then 10 || theAmountOfApples == 100) sell your apples. I dont write c# or c++ but I did learn it before I learned java and they are close languages.

    0 讨论(0)
  • 2020-11-22 07:49

    The switch statement is definitely the faster then a if else if. There are speedtest that have been supplied on it by BlackWasp

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

    --Check it out

    But depends heavily on the possibilities that you're trying to account for, but I try to use a switch statement whenever possible.

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