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.

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. for (int i = 0; i < loop; i++)
  7. {
  8. sw.Start();
  9. DecideDayIfFirst();
  10. DecideDayIfLast();
  11. DecideDayIfSimpleFirst();
  12. DecideDayIfSimpleLast();
  13. DayComparisonSwitchFirst();
  14. DayComparisonSwitchLast();
  15. sw.Stop();
  16. ticksSum = ticksSum + sw.ElapsedTicks;
  17. sw.Reset();
  18. }
  19. Console.WriteLine($"{ticksSum / loop}");
  20. }
  21. Console.ReadLine();
  22. }
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. for (int i = 0; i < SampleSize; i++)
  6. {
  7. if (day == "Saturday")
  8. {
  9. dayType = "Weekend";
  10. }
  11. if (day == "Sunday")
  12. {
  13. dayType = "Weekend";
  14. }
  15. if (day == "Monday")
  16. {
  17. dayType = "Weekday";
  18. }
  19. if (day == "Tuesday")
  20. {
  21. dayType = "Weekday";
  22. }
  23. if (day == "Wednesday")
  24. {
  25. dayType = "Weekday";
  26. }
  27. if (day == "Thursday")
  28. {
  29. dayType = "Weekday";
  30. }
  31. if (day == "Friday")
  32. {
  33. dayType = "Weekday";
  34. }
  35. else
  36. {
  37. dayType = "Something Wrong !!!";
  38. }
  39. }
  40. }
  41. static void DecideDayIfSimpleLast()
  42. {
  43. string day = "Saturday";
  44. string dayType = string.Empty;
  45. for (int i = 0; i < SampleSize; i++)
  46. {
  47. if (day == "Sunday")
  48. {
  49. dayType = "Weekend";
  50. }
  51. if (day == "Monday")
  52. {
  53. dayType = "Weekday";
  54. }
  55. if (day == "Tuesday")
  56. {
  57. dayType = "Weekday";
  58. }
  59. if (day == "Wednesday")
  60. {
  61. dayType = "Weekday";
  62. }
  63. if (day == "Thursday")
  64. {
  65. dayType = "Weekday";
  66. }
  67. if (day == "Friday")
  68. {
  69. dayType = "Weekday";
  70. }
  71. if (day == "Saturday")
  72. {
  73. dayType = "Weekend";
  74. }
  75. else
  76. {
  77. dayType = "Something Wrong !!!";
  78. }
  79. }
  80. }
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. for (int i = 0; i < SampleSize; i++)
  6. {
  7. if (day == "Saturday")
  8. {
  9. dayType = "Weekend";
  10. }
  11. else if (day == "Sunday")
  12. {
  13. dayType = "Weekend";
  14. }
  15. else if (day == "Monday")
  16. {
  17. dayType = "Weekday";
  18. }
  19. else if (day == "Tuesday")
  20. {
  21. dayType = "Weekday";
  22. }
  23. else if (day == "Wednesday")
  24. {
  25. dayType = "Weekday";
  26. }
  27. else if (day == "Thursday")
  28. {
  29. dayType = "Weekday";
  30. }
  31. else if (day == "Friday")
  32. {
  33. dayType = "Weekday";
  34. }
  35. else
  36. {
  37. dayType = "Something Wrong !!!";
  38. }
  39. }
  40. }
  41. static void DecideDayIfLast()
  42. {
  43. string day = "Saturday";
  44. string dayType = string.Empty;
  45. for (int i = 0; i < SampleSize; i++)
  46. {
  47. if (day == "Sunday")
  48. {
  49. dayType = "Weekend";
  50. }
  51. else if (day == "Monday")
  52. {
  53. dayType = "Weekday";
  54. }
  55. else if (day == "Tuesday")
  56. {
  57. dayType = "Weekday";
  58. }
  59. else if (day == "Wednesday")
  60. {
  61. dayType = "Weekday";
  62. }
  63. else if (day == "Thursday")
  64. {
  65. dayType = "Weekday";
  66. }
  67. else if (day == "Friday")
  68. {
  69. dayType = "Weekday";
  70. }
  71. else if (day == "Saturday")
  72. {
  73. dayType = "Weekend";
  74. }
  75. else
  76. {
  77. dayType = "Something Wrong !!!";
  78. }
  79. }
  80. }
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. static void DayComparisonSwitchLast()
  36. {
  37. string dayType = string.Empty;
  38. string day = "Saturday";
  39. for (int i = 0; i < SampleSize; i++)
  40. {
  41. switch (day)
  42. {
  43. case "Sunday":
  44. dayType = "Weekend";
  45. break;
  46. case "Monday":
  47. dayType = "Weekday";
  48. break;
  49. case "Thursday":
  50. dayType = "Weekday";
  51. break;
  52. case "Tuesday":
  53. dayType = "Weekday";
  54. break;
  55. case "Wednesday":
  56. dayType = "Weekday";
  57. break;
  58. case "Friday":
  59. dayType = "Weekday";
  60. break;
  61. case "Saturday":
  62. dayType = "Weekend";
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. }
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

When to use what?

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