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

Introduction

In the past week we said that in Office the best for performance is either between the if statement or the switch statement. I prefer normally an if statement. But I don't know which is the best for performance. So I decided to make a simple program to determine which condition is best. Ok let's check with me. 

 
NoteOutput response time may be differ. It depends on your processor and RAM. 
 
The program output depends on our processor and the RAM. I have used I-3. I shared our system summary information.
 
 
 
I made a Console based application. This application has one single class and four methods.
  1. static void Main(string[] args)  
  2. {  
  3.       Program p = new Program();  
  4.       p.GetPerformance(); //first Performance method called here  
  5.       p.Getperformance2();  // second Performance Method Called here  
  6.       p.GetPerformanceIfcondition();  
  7.       p.GetPerformanceSwitchCondition();  

Everyone asks the one question, what provides the best performance?

Example One
  1. if(condtion1==true)  
  2. {  
  3.    if(condition2==true)  
  4.    {  
  5.       if(condition3==true)  
  6.       {  
  7.          Console.WriteLine("Every Condition is True");  
  8.       }  
  9.    }  

Example Two
  1. if(condtion1==true && condition2==true && condition3==true)  
  2. {  
  3.    Console.WriteLine("Every Condition is True");  

We can't assume which one provides the best performance. So I made two separate methods and called them one by one and got the difference in performance. 
  1. p.GetPerformance(); //Example One call here  
You can check the following Getperformance that has one loop and this loop continues looping 1000000000 times.  Every time the condition is checked three times. If the first condition is equal to 10 then the compiler goes deeper. Then it again checks the condition for the second condition equal to 11 then the compiler goes deeper. The same process is called until the for loop gets a false condition.
 
Set a staring time.
  1. string startingTime = DateTime.Now.ToLongTimeString();   
Condition check every time.
  1. if (firstcondition == 10)    
  2. {    
  3.     if (secondcondition == 11)    
  4.     {    
  5.         if (Thridcondition == 12)    
  6.         {    
  7.    
  8.         }    
  9.     }    
  10. }   
Set the Endtime.
  1. endtime = DateTime.Now.ToLongTimeString();   
The complete method is here.
  1. public void GetPerformance()  
  2. {  
  3.       Console.WriteLine("First Method");   //This line execute once time only.
  4.       int firstcondition = 10, secondcondition = 11, Thridcondition = 12; 
  5.       string endtime = DateTime.Now.ToLongTimeString();  
  6.       string startingTime = DateTime.Now.ToLongTimeString();  
  7.       for (int i = 0; i < 1000000000; i++)  
  8.       {  
  9.           if (firstcondition == 10)  
  10.           {  
  11.               if (secondcondition == 11)  
  12.               {  
  13.                   if (Thridcondition == 12)  
  14.                   {  
  15.  
  16.                   }  
  17.               }  
  18.           }  
  19.       }  
  20.       endtime = DateTime.Now.ToLongTimeString();  
  21.       TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
  22.       Console.WriteLine(t1);  
  23.   }  
Example Two
 
The Getperformance2 call is here.
  1. p.Getperformance2(); 
The same condition is here but some formating has been changed.
  1. if (firstcondition == 10 && secondcondition == 11 && Thridcondition == 12)    
  2. {    
  3.   
  4. }   
The loop again works at the same time. 
  1. for (int i = 0; i < 1000000000; i++)    
  2. {    
  3.     if (firstcondition == 10 && secondcondition == 11 && Thridcondition == 12)    
  4.     {    
  5.    
  6.     }    
  7. }   
The complete method is here. 
  1. public void Getperformance2()  
  2. {  
  3.     Console.WriteLine("second Method");  
  4.     int firstcondition = 10, secondcondition = 11, Thridcondition = 12;  
  5.     string endtime = DateTime.Now.ToShortTimeString();  
  6.     string startingTime = DateTime.Now.ToLongTimeString();  
  7.     for (int i = 0; i < 1000000000; i++)  
  8.     {  
  9.         if (firstcondition == 10 && secondcondition == 11 && Thridcondition == 12)  
  10.         {  

  11.         }  
  12.     }  
  13.     endtime = DateTime.Now.ToLongTimeString();  
  14.     TimeSpan t2 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
  15.  
  16.     Console.WriteLine(t2);  
  17.   

Output of both methods

You can check whether both methods were called here and show the difference between both methods. One method is take the time of five seconds and another method is take the time of four seconds. So we can easily decide which is the best for performance. 
 
 

Final Word

I hope this small article is useful for everyone. If you have any query or problem about this article and the related code then please do not hesitate to drop your comment in the comment box.

Up Next
    Ebook Download
    View all
    Learn
    View all