First Look at Performance of IF and Conditional Expression: Part 5

Background

In this article am trying solve one more Performance issue IF condition and Conditional Expression. In office today debut on this about which is best so I decided make a simple program and got the Result which best.
 
 
 
Performance

Every human think about speed means performance is want in every where like Internet speed, LP speed and so on. So this article based on Performance issue. 
 
IF Condition                    
  1. string HoldValue=string.Empty;
  2. If(HoldValue==string.Empty)  
  3. {  
  4.    do stuff here ////// if condition is True
  5.    Holdvalue="Test Value";

  6. else
  7. {
  8.    do stuff here///// if Condition is False
  9.    HoldValue="";
  10. }
Conditional Expression
  1. string HoldValue=string.Empty;  
  2. HoldValue=HoldValue==string.string.Empty ? "Test Value" : "";   
Condition Expression is sign ? (In formal way we could called question mark)
 
Expression of Conditional Expression.
  1. Variable=conditional Value==Condition ? "Assign this if Condition is true: Assign this if Condition is False; 
Compare performance between If and Conditional Expression
We are used the native code for performance checked like a For Loop. I have used one single console application.
  1. static void Main(string[] args)  
  2. {  
  3.       Program p = new Program();        
  4.       p.SimpleIfElse();  
  5.       p.ConditionalExpression();            

You can check above example have Two method is "SimpleIfElse" and "ConditionalExpression".  
 
Method SimpleIfElse
  1. public void SimpleIfElse()  
  2. {  
  3.       Console.WriteLine("Don't worry be Relex your System is Ok ☺");  
  4.       Console.WriteLine("SimpleIfElse Call here");  
  5.       string startingTime = DateTime.Now.ToLongTimeString();  
  6.       for (int i = 0; i < 1000000000; i++)  
  7.       {  
  8.           if (HoldTempValue == string.Empty)  
  9.           {  
  10.               HoldTempValue = "TestingValue";  
  11.   
  12.           }  
  13.           HoldTempValue = string.Empty;  
  14.       }  
  15.             
  16.       string  endtime = DateTime.Now.ToLongTimeString();  
  17.       TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
  18.       Console.WriteLine(t1);  
  19. }  
You can above see IfElse condition. In this method have used one for loop. This loop execute continue until reach the last number. 
 
Start Time set here.
  1. string startingTime = DateTime.Now.ToLongTimeString();  
End time set here.
  1. string endtime = DateTime.Now.ToLongTimeString();  
Get the difference between start time and end time.
  1. TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
Method Conditional Expression
  1. public void ConditionalExpression()  
  2. {  
  3.      Console.WriteLine("ConditionalExpression call here");  
  4.      string endtime = DateTime.Now.ToLongTimeString();  
  5.      string startingTime = DateTime.Now.ToLongTimeString();  
  6.   
  7.      for (int i = 0; i < 1000000000; i++)  
  8.      {  
  9.          HoldTempValue = HoldTempValue == string.Empty ? "TestingValue" : "";  
  10.          HoldTempValue = string.Empty;  
  11.   
  12.      }  
  13.      endtime = DateTime.Now.ToLongTimeString();  
  14.      TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));  
  15.      Console.WriteLine(t1);                     
  16. }  
Final output of this small program:- 
 
 
 
Note:- Time is depend on your Machine Performance. 

Final Word

I hope this small program will help in your programming acknowledge about the IF and Conditional Expression. If have any query free feel drop your comment in comment box.  

Up Next
    Ebook Download
    View all
    Learn
    View all