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. else
  9. if (month == 3)
  10. {
  11. }

The complete method is placed here.
  1. public void GetPerformanceIfcondition()
  2. {
  3. Console.WriteLine("If else performance check");
  4. string endtime = DateTime.Now.ToShortTimeString();
  5. string startingTime = DateTime.Now.ToLongTimeString();
  6. Random rnd = new Random();
  7. for (int i = 0; i < 10000000; i++)
  8. {

  9. int month = rnd.Next(1, 13); // creates a number between 1 and 12
  10. if (month == 1)
  11. {
  12. }
  13. else
  14. if (month == 2)
  15. {
  16. }
  17. else
  18. if (month == 3)
  19. {
  20. }
  21. else
  22. if (month == 4)
  23. {
  24. }
  25. else
  26. if (month == 5)
  27. {
  28. }
  29. else
  30. if (month == 6)
  31. {
  32. }
  33. else
  34. if (month == 7)
  35. {
  36. }
  37. else
  38. if (month == 8)
  39. {
  40. }
  41. else
  42. if (month == 9)
  43. {
  44. }
  45. else
  46. if (month == 10)
  47. {
  48. }
  49. else
  50. if (month == 11)
  51. {
  52. }
  53. if (month == 12)
  54. {
  55. }
  56. }
  57. endtime = DateTime.Now.ToLongTimeString();
  58. TimeSpan t2 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));
  59. Console.WriteLine(t2);
  60. }
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. case 2:
  6. break;
  7. case 3:
  8. break;
  9. // 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.