C# if else vs switch, which efficiency is high(multi-branch efficiency comparison)

Lionsure 2020-07-09 Original by the website

There are two types of multi-branch statements in C#, one is if elseif and the other is switch. Which one should be used should be determined according to the specific situation and execution efficiency of program in the programming process. This article will discuss which switch and if elseif are more efficient, the following is a specific analysis.

 

C# if else vs switch, which efficiency is high? First look at the code implemented by the two statements:

//Page initialization
       protected void Page_Load(object sender, EventArgs e)
       {
              If_Elseif(3);
              Switch_Case(3);
       }

//Test if elseif
       public string If_Elseif(int n)
       {
              string returnValue = "Lionsure";

       Stopwatch sw = new Stopwatch();
              sw.Start();

       for (int i = 0; i < 10000000; i++)
              {
                     if (n == 1)
                            returnValue = "Page can not open";
                     else if (n == 2)
                            returnValue = "Webpage is opened slowly";
                     else if (n == 3)
                            returnValue = "The webpage is not displayed properly";
                     else if (n == 4)
                            returnValue = "The webpage displays garbled characters";
                     else if (n == 5)
                            returnValue = "Page can not open in Ie";
                     else if (n == 6)
                            returnValue = "Page can not open in Chrome";
              }
              sw.Stop();
              lblRunTime.Text = " If_Elseif: " + sw.ElapsedMilliseconds;//label tag
              return returnValue;
       }

//Test Switch case
       public string Switch_Case(int n)
       {
              string returnValue = "Lionsure";

       Stopwatch sw = new Stopwatch();
              sw.Start();

       for (int i = 0; i < 10000000; i++)
              {
                     switch (n)
                     {
                            case 1:
                                   returnValue = "Page can not open";
                                   break;
                            case 2:
                                   returnValue = "Webpage is opened slowly";
                                   break;
                            case 3:
                                   returnValue = "The webpage is not displayed properly";
                                   break;
                            case 4:
                                   returnValue = "The webpage displays garbled characters";
                                   break;
                            case 5:
                                   returnValue = "Page can not open in Ie";
                                   break;
                            case 6:
                                   returnValue = "Page can not open in Chrome";
                                   break;
                            default:
                                   break;
                     }
              }
              sw.Stop();
              lblRunTime.Text += "ms Switch_Case: " + sw.ElapsedMilliseconds + "ms";
              return returnValue;
       }

Operation result:

If_Elseif: 162ms Switch_Case: 71ms

It can be seen from the running results that if elseif takes 91ms more time than the switch, the program loops 10,000,000 times. If the number of cycles continues to increase, the efficiency of switch is more obvious. It can be seen that the efficiency of switch is higher than if elseif.

The switch is fast because it adds an index to jump after compilation, and if ... elseif does not increase the index to jump. Looking at the decompiled ildasm code, you can find the following sentence added to the Switch_Case method:

switch (IL_003f, IL_0047, IL_004f, IL_0057, IL_005f, IL_0067)

It implements a jump table.