
C# if else vs switch, which efficiency is high(multi-branch efficiency comparison)
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.
-
Related Reading
- C# Read and write to text file Newline, with one lin
- Bubble sort algorithm example in C# and C (Ascending
- C# label control, with winforms label transparent ba
- C# Richtextbox change font color, add and foreach li
- C# sort files or folders by name, date and creation
- Three methods of Hashtable sort by key or value in C
- C# Windows Forms foreach controls, with Textbox and
- C# sorted dictionary by value, key or length of the
- C# Winforms panel border, with color, style, thickne
- C# Winforms listview show table(adaptive column widt
- Selection sort algorithm C# and C (Ascending and Des
- Quick sort algorithm in C and C# (Ascending and Desc