Performance Consideration For C# Conditional Statements

In our C# programming life, we use If-Else if, Switch case, and If conditional statements frequently. In this article, we will look into the performance of each of these conditional statements in C#.

The code used in this exercise is available here on GitHub for experimenting.

In our C# programming life, we use If-Else if, Switch case, and If conditional statements frequently. If you just start using them by putting conditions in a random order, please wait. This six-minute read will change your approach and by the end of this article, you will have the foolproof answers to the following questions.

  • Is there any performance difference among them?
  • Which one is better to use?
  • Does the ordering of conditions impact the performance? If Yes, how?
  • When to use what?

If these questions are confusing to you, don't worry. We will walk through the scenarios and perform a comparative study of their performances to demystify the secrets.

Approach

Scenario

Here, we are going to decide if the passing day is Weekday or Weekend. For this, we will be using If-else if, Switch case, and simple If conditions.

Analysis

We will put the true condition in the beginning and then, in the last in another method for each conditional statement.

In order to evaluate the performance in each case, we are going to run this 100,000 times and then this loop for 1000 times and take an average of this. Again, we will be running the whole process 5 times and I will share the findings and results. This has been done in this code. The complete code is available on GitHub for more experimenting.

  1. static void Main(string[] args)  
  2. {  
  3.     Stopwatch sw = new Stopwatch();  
  4.     for (int x = 0; x < 5; x++)  
  5.     {  
  6.   
  7.   
  8.         for (int i = 0; i < loop; i++)  
  9.         {  
  10.             sw.Start();  
  11.             DecideDayIfFirst();  
  12.             DecideDayIfLast();  
  13.   
  14.             DecideDayIfSimpleFirst();  
  15.             DecideDayIfSimpleLast();  
  16.   
  17.             DayComparisonSwitchFirst();  
  18.             DayComparisonSwitchLast();  
  19.   
  20.             sw.Stop();  
  21.             ticksSum = ticksSum + sw.ElapsedTicks;  
  22.             sw.Reset();  
  23.         }  
  24.         Console.WriteLine($"{ticksSum / loop}");  
  25.     }  
  26.     Console.ReadLine();  
  27. }  
I am using ticks here to evaluate the time, as I have considered memory to be available and time is precious.

If Condition

This is the basic If condition where we will have multiple if conditions.

Code Sample

  1. static void DecideDayIfSimpleFirst()  
  2. {  
  3.     string day = "Saturday";  
  4.     string dayType = string.Empty;  
  5.   
  6.     for (int i = 0; i < SampleSize; i++)  
  7.     {  
  8.         if (day == "Saturday")  
  9.         {  
  10.             dayType = "Weekend";  
  11.         }  
  12.         if (day == "Sunday")  
  13.         {  
  14.             dayType = "Weekend";  
  15.         }  
  16.         if (day == "Monday")  
  17.         {  
  18.             dayType = "Weekday";  
  19.         }  
  20.         if (day == "Tuesday")  
  21.         {  
  22.             dayType = "Weekday";  
  23.         }  
  24.         if (day == "Wednesday")  
  25.         {  
  26.             dayType = "Weekday";  
  27.         }  
  28.         if (day == "Thursday")  
  29.         {  
  30.             dayType = "Weekday";  
  31.         }  
  32.         if (day == "Friday")  
  33.         {  
  34.             dayType = "Weekday";  
  35.         }  
  36.         else  
  37.         {  
  38.             dayType = "Something Wrong !!!";  
  39.         }  
  40.     }  
  41. }  
  42. static void DecideDayIfSimpleLast()  
  43. {  
  44.     string day = "Saturday";  
  45.     string dayType = string.Empty;  
  46.   
  47.     for (int i = 0; i < SampleSize; i++)  
  48.     {  
  49.   
  50.         if (day == "Sunday")  
  51.         {  
  52.             dayType = "Weekend";  
  53.         }  
  54.         if (day == "Monday")  
  55.         {  
  56.             dayType = "Weekday";  
  57.         }  
  58.         if (day == "Tuesday")  
  59.         {  
  60.             dayType = "Weekday";  
  61.         }  
  62.         if (day == "Wednesday")  
  63.         {  
  64.             dayType = "Weekday";  
  65.         }  
  66.         if (day == "Thursday")  
  67.         {  
  68.             dayType = "Weekday";  
  69.         }  
  70.         if (day == "Friday")  
  71.         {  
  72.             dayType = "Weekday";  
  73.         }  
  74.         if (day == "Saturday")  
  75.         {  
  76.             dayType = "Weekend";  
  77.         }  
  78.         else  
  79.         {  
  80.             dayType = "Something Wrong !!!";  
  81.         }  
  82.     }  
  83. }  
Result

More data about this analysis is available with the code repository. Unit of measurement is elapsed ticks from the stopwatch. Since multiple if statements evaluate all expressions, we don't see much difference in their performance.

When true condition is in the First
(A)
When true condition is in the Last
(B)
Performance
(B/A)
5315 5425 1.020696
10398 10696 1.028659
15622 15924 1.019332
20843 21144 1.014441
26015 26307 1.011224

If-Else if Condition

For this, we have the following code.

Code Sample

  1. static void DecideDayIfFirst()  
  2. {  
  3.     string day = "Saturday";  
  4.     string dayType = string.Empty;  
  5.   
  6.     for (int i = 0; i < SampleSize; i++)  
  7.     {  
  8.         if (day == "Saturday")  
  9.         {  
  10.             dayType = "Weekend";  
  11.         }  
  12.         else if (day == "Sunday")  
  13.         {  
  14.             dayType = "Weekend";  
  15.         }  
  16.         else if (day == "Monday")  
  17.         {  
  18.             dayType = "Weekday";  
  19.         }  
  20.         else if (day == "Tuesday")  
  21.         {  
  22.             dayType = "Weekday";  
  23.         }  
  24.         else if (day == "Wednesday")  
  25.         {  
  26.             dayType = "Weekday";  
  27.         }  
  28.         else if (day == "Thursday")  
  29.         {  
  30.             dayType = "Weekday";  
  31.         }  
  32.         else if (day == "Friday")  
  33.         {  
  34.             dayType = "Weekday";  
  35.         }  
  36.   
  37.         else  
  38.         {  
  39.             dayType = "Something Wrong !!!";  
  40.         }  
  41.     }  
  42. }  
  43. static void DecideDayIfLast()  
  44. {  
  45.     string day = "Saturday";  
  46.     string dayType = string.Empty;  
  47.   
  48.     for (int i = 0; i < SampleSize; i++)  
  49.     {  
  50.   
  51.         if (day == "Sunday")  
  52.         {  
  53.             dayType = "Weekend";  
  54.         }  
  55.         else if (day == "Monday")  
  56.         {  
  57.             dayType = "Weekday";  
  58.         }  
  59.         else if (day == "Tuesday")  
  60.         {  
  61.             dayType = "Weekday";  
  62.         }  
  63.         else if (day == "Wednesday")  
  64.         {  
  65.             dayType = "Weekday";  
  66.         }  
  67.         else if (day == "Thursday")  
  68.         {  
  69.             dayType = "Weekday";  
  70.         }  
  71.         else if (day == "Friday")  
  72.         {  
  73.             dayType = "Weekday";  
  74.         }  
  75.         else if (day == "Saturday")  
  76.         {  
  77.             dayType = "Weekend";  
  78.         }  
  79.         else  
  80.         {  
  81.             dayType = "Something Wrong !!!";  
  82.         }  
  83.     }  
  84. }  
Results

More data about this analysis is available with the code repository. Unit of measurement is elapsed ticks from the stopwatch. First case true performs approx 6 times faster than the last case true in if- else if condition.

When true in First (A) When true in Last (B) Performance (B/A)
942 5364 5.6942675
1768 10489 5.9326923
2481 15670 6.3160016
3159 20765 6.5732827
3825 25886 6.7675817

Switch Case

With the switch case also, we will be doing the same test as follows,

Code Sample

  1. static void DayComparisonSwitchFirst()  
  2. {  
  3.     string dayType = string.Empty;  
  4.     string day = "Saturday";  
  5.     for (int i = 0; i < SampleSize; i++)  
  6.     {  
  7.         switch (day)  
  8.         {  
  9.             case "Saturday":  
  10.                 dayType = "Weekend";  
  11.                 break;  
  12.             case "SundayS":  
  13.                 dayType = "Weekend";  
  14.                 break;  
  15.             case "Monday":  
  16.                 dayType = "Weekday";  
  17.                 break;  
  18.             case "Thursday":  
  19.                 dayType = "Weekday";  
  20.                 break;  
  21.             case "Tuesday":  
  22.                 dayType = "Weekday";  
  23.                 break;  
  24.             case "Wednesday":  
  25.                 dayType = "Weekday";  
  26.                 break;  
  27.             case "Friday":  
  28.                 dayType = "Weekday";  
  29.                 break;  
  30.             default:  
  31.                 break;  
  32.         }  
  33.     }  
  34.   
  35. }  
  36. static void DayComparisonSwitchLast()  
  37. {  
  38.     string dayType = string.Empty;  
  39.     string day = "Saturday";  
  40.     for (int i = 0; i < SampleSize; i++)  
  41.     {  
  42.         switch (day)  
  43.         {  
  44.             case "Sunday":  
  45.                 dayType = "Weekend";  
  46.                 break;  
  47.             case "Monday":  
  48.                 dayType = "Weekday";  
  49.                 break;  
  50.             case "Thursday":  
  51.                 dayType = "Weekday";  
  52.                 break;  
  53.             case "Tuesday":  
  54.                 dayType = "Weekday";  
  55.                 break;  
  56.             case "Wednesday":  
  57.                 dayType = "Weekday";  
  58.                 break;  
  59.             case "Friday":  
  60.                 dayType = "Weekday";  
  61.                 break;  
  62.             case "Saturday":  
  63.                 dayType = "Weekend";  
  64.                 break;  
  65.             default:  
  66.                 break;  
  67.         }  
  68.     }  
  69. }  
Result

More data is available with code repository In Switch case, we can see there is not much difference in their performances on the basis of order. It is almost the same.

First case is true (A) Last case is true (B) Diff (B/A)
9142 9137 .9994531
17945 17833 .9937587
26599 26957 1.013459
35236 35711 1.013481
43943 44327 1.008739

Conclusion

With the above study, we can easily conclude the following and use them in our assignment,

Placement order of "true condition" impacts the performance

  • As we saw here, we compared the true with the extreme scenario and figured out the impact on performance. Hence try to place the conditions in order of their frequency.
  • We see here, if-else if has the biggest performance difference (up to 6-7 times) while Switch case doesn't differ much and it is almost the same with the placement of condition.
  • Having "Multiple If conditions" impacts the most as it evaluates all conditions. If-else if doesn't evaluate once it finds the true condition and Switch case evaluates only the true case.

When to use what?

  • When we are certain about the frequency and order of condition, use if-else if.
  • When we don't know the frequency and it could be random, still if-else if has better performance. See the ticks elapsed in the last condition If-else if vs first condition switch case from the above table.
  • Do NOT use multiple If statements, as it will impact the performance as it will evaluate the expression again and again even after true condition is met. Hence we don't see much difference in performance based on order. Similarly, we don't see a performance difference with Switch but if we compare Switch with multiple if, multiple if gets an edge.
  • So the preference of C# Conditional Statements goes in this order - #1 - If- Else If -> #2 Multiple If -> #3 Switch case.
  • If there are not many conditions and they are straightforward then Switch case can be considered as it provides better readability over either of the If conditions and on a performance front doesn't impose a huge penalty.
  • If we need to use Switch case for pattern matching (as we have done in this example) and if there are too many scenarios then dictionary with the query can be a better contestant for this scenario.
  • When we have multiple OR expressions in conditional expressions the Switch can be more readable than If-else if. The example used in this article can be re-written and we will analyze the performance as well.

  1. static void DayComparisonMultiORIf()  
  2. {  
  3.     string day = "Friday";//"Saturday";    
  4.     string dayType = string.Empty;  
  5.   
  6.   
  7.     for (int i = 0; i < SampleSize; i++)  
  8.     {  
  9.         if (day == "Saturday" || day == "Sunday")  
  10.         {  
  11.             dayType = "Weekend";  
  12.         }  
  13.         else if (day == "Monday" || day == "Tuesday"  
  14.             || day == "Wednesday" || day == "Thursday" || day == "Friday")  
  15.         {  
  16.             dayType = "Weekday";  
  17.         }  
  18.         else  
  19.         {  
  20.             dayType = "Something Wrong !!!";  
  21.         }  
  22.     }  
  23. }  
  24. static void DayComparisonMultiORSwitch()  
  25. {  
  26.     string dayType = string.Empty;  
  27.     string day = "Friday";//"Saturday";    
  28.     for (int i = 0; i < SampleSize; i++)  
  29.     {  
  30.         switch (day)  
  31.         {  
  32.             case "Sunday":  
  33.             case "Saturday":  
  34.                 dayType = "Weekend";  
  35.                 break;  
  36.             case "Monday":  
  37.             case "Tuesday":  
  38.             case "Wednesday":  
  39.             case "Thursday":  
  40.             case "Friday":  
  41.                 dayType = "Weekday";  
  42.                 break;  
  43.             default:  
  44.                 dayType = "Soething wrong !!";  
  45.                 break;  
  46.         }  
  47.     }  
  48. }  
Performance

In this way we can see the data and here again if-else if is the clear winner. When the first condition meets true then switch case takes approx 12 times more than If-else if. On the other hand, if Last condition is true then also If- else if has a gain of 1.5 time faster processing.

First True Last True
If Switch Diff if switch Diff
770 9209 11.96 5775 8190 1.42
1529 18358 12.01 11296 16119 1.43
2296 27437 11.95 16823 23689 1.41
2995 36665 12.24 22364 31817 1.43

Disclaimer

For this analysis, I have used an extreme case scenario as in first and last, to show the significant difference in performance and comparison. Your real life scenario may differ but I hope that this article gives you a better understanding of how and what to use from conditional statements in C#.

Share your feedback and download the code to experiment further.