First Look At Performance Of If and Switch Statements: Part 2

The last article attempted to evaluate the performance of an if statement, you can check this link for more information about the if statement performance. In this article we will analyze what provides the best performance, the if statement or the switch statement. I have prefered the if statement. 
 
I have created one Console Application for testing the if statements and switch statements.
  1. Program p = new Program(); // Class Object create  
  2.            p.GetPerformanceIfcondition();  // Call if else condition   
  3.            p.GetPerformanceSwitchCondition(); //Call switch case Condition 
Exploring the if with else
  1. p.GetPerformanceIfcondition();  // Call if else condition    
Set the method start time.  
  1. string startingTime = DateTime.Now.ToLongTimeString(); // When this method call starting time assign the value of starting time.    
Loop to check the performance. This loop continues looping until the i exceeds this 10000000 value.
  1.  for (int i = 0; i < 10000000; i++)    
  2. {  
  3.    

Get a random number for the if statement. Random is a predefined class, in the class is the next function, this function returns the one single value. Random one single value. rnd.Next('seed value','max value). This function returns the value every time between 1 and 12.  
  1. Random rnd = new Random();    
  2. int month = rnd.Next(1, 13); // creates a number between 1 and 12   
If statement in a loop. 
  1. if (month == 1)    
  2. {    
  3. }    
  4. else    
  5. if (month == 2)    
  6. {    
  7.     
  8. }    
  9. else    
  10. if (month == 3)    
  11. {    
  12. }    
  13.                    
The complete method is placed here.
  1. public void GetPerformanceIfcondition()  
  2.   {  
  3.   
  4.       Console.WriteLine("If else performance check");  
  5.       string endtime = DateTime.Now.ToShortTimeString();  
  6.       string startingTime = DateTime.Now.ToLongTimeString();
  7.       Random rnd = new Random();
     
  8.       for (int i = 0; i < 10000000; i++)  
  9.       {  
  10.          
  11.           int month = rnd.Next(1, 13); // creates a number between 1 and 12  
  12.           if (month == 1)  
  13.           {  
  14.           }  
  15.           else  
  16.               if (month == 2)  
  17.               {  
  18.   
  19.               }  
  20.               else  
  21.                   if (month == 3)  
  22.                   {  
  23.                   }  
  24.                   else  
  25.                       if (month == 4)  
  26.                       {  
  27.                       }  
  28.                       else  
  29.                           if (month == 5)  
  30.                           {  
  31.                           }  
  32.                           else  
  33.                               if (month == 6)  
  34.                               {  
  35.                               }  
  36.                               else  
  37.                                   if (month == 7)  
  38.                                   {  
  39.                                   }  
  40.                                   else  
  41.                                       if (month == 8)  
  42.                                       {  
  43.                                       }  
  44.                                       else  
  45.                                           if (month == 9)  
  46.                                           {  
  47.                                           }  
  48.                                           else  
  49.                                               if (month == 10)  
  50.                                               {  
  51.                                               }  
  52.                                               else  
  53.                                                   if (month == 11)  
  54.                                                   {  
  55.                                                   }  
  56.                                                   if (month == 12)  
  57.                                                   {  
  58.                                                   }  
  59.   
  60.       }  
  61.       endtime = DateTime.Now.ToLongTimeString();  
  62.       TimeSpan t2 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
  63.       Console.WriteLine(t2);  
  64.   
  65.   
  66.   } 
Switch Statement
 
We have already defined the preceding. We can also comare the performance between an if statement and a switch statement. 
  1.  public void GetPerformanceSwitchCondition()  
  2. {
  3.  

The following is a for loop of the same as in the if statement.
 
  1.  for (int i = 0; i < 10000000; i++)  
  2.             {  
  3.   

 
In this function have used the switch statement. 
 
  1. switch (month)  
  2. {  
  3.    case 1:  
  4.    break;  
  5.   
  6.    case 2:  
  7.    break;  
  8.   
  9.    case 3:  
  10.    break
  11.             // Case 1: to Case 12:
The output of both functions are run in a console based application.
 
 

Conclusion

From this article we hope to understand what provides the best performance. You can check it yourself. I have attached one console application. For any query provide you comments. Every  comment is welcome. 
 
Note: Live Demo: .NET Fiddle.