BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Performance
.NET 2.0+

Speed Test: Switch vs If-Else-If

The .NET framework and the C# language provide two methods for conditional processing where multiple discrete values can be selected from. The switch statement is less flexible than the if-else-if ladder but is generally considered to be more efficient.

Test Description

Purpose

This test was designed to compare the execution time of the switch statement and the if-else-if ladder code structure when used to select a match from a discrete series of values. The if-else-if ladder provides much greater flexibility than is used by the test but, when being used to provide matching functionality to switch, this does not need to be considered. The two conditional processing structures are described in the article, C# Program Flow Control: Conditional Processing.

Test Process Functionality

It was important that the speed test for the two statements provided exactly the same functionality in both cases. For each test, an integer value between zero and nineteen was generated. This integer value was then compared against nineteen possible selections using if commands and case statements. Where the integer value generated was nineteen, this was not matched and instead processed using the final else statement or the switch command's default case.

In many programming situations, a selection of this kind is very likely to generate a match. However, for completeness, a second test was performed. This test followed the same functionality but using an integer value that did not match any of the possible values; it therefore always used the final else statement or the switch command's default case.

Looping

The speed of execution of a large if-else-if ladder or select statement is too fast to be accurately measured. To make measuring possible and to reduce anomalies, a loop was constructed and the test code executed repeatedly. For each test, the loop completed one billion (1,000,000,000) iterations.

The looping function for the first test was used to generate the integer value to match. For each iteration in a for loop, the loop control variable was used with the modulus operator to create a comparison value between zero and nineteen.

In addition to executing the test with value-matching and the test without value-matching, the loop code was executed with no internal test code. Two versions of empty loop were used, one with the number generation code and one without. These two tests provided baseline timings that could be subtracted from the actual test times.

Timing

The timing of the tests was controlled automatically using the Stopwatch class. Each test was performed repeatedly and the mean average of the results calculated.

Test Conditions

Hardware

The test results included below are those produced using an Athlon64 3200+ with 4GB RAM. These tests are indicative of further relative test results that were carried out on a variety of equipment including:

  • IBM ThinkPad R51 notebook with a 1.6GHz processor and 2GB RAM
  • JVC MiniNote notebook with a 1GHz processor and 768MB RAM

The tests were executed using three operating systems, each with the latest service packs and patches. These were:

  • Windows XP
  • Windows Server 2003 R2
  • Windows Vista Ultimate

In each test, the software was compiled as a .NET framework 2.0 console application in three configurations:

  • Compiled for 32-bit processors only using Visual Studio 2005 Team Edition
  • Compiled for 32-bit or 64-bit processors using Visual Studio 2005 Team Edition
  • Compiled using Visual C# 2005 Express Edition

Results

Raw Results

This table shows the timings for the empty loop, if statement and switch statement for one billion iterations rounded to the nearest millisecond. The two columns show the results for the loops where matching of the comparison value occurs and for the non-matching tests.

MatchingNon-Matching
Empty Loop27.3s5.0s
Switch Statement43.0s5.0s
If Statement48.0s5.1s

Adjusted Results

This second table shows the results for the two key test types, adjusted to remove the delay created by the looping mechanism.

MatchingNon-Matching
Switch Statement15.7s0.0s
If Statement20.7s0.1s

Conclusion

The results show that the switch statement is faster to execute than the if-else-if ladder. This is due to the compiler's ability to optimise the switch statement. In the case of the if-else-if ladder, the code must process each if statement in the order determined by the programmer. However, because each case within a switch statement does not rely on earlier cases, the compiler is able to use branch tables in such a way as to provide the fastest execution.

When creating your own software you are unlikely to see a real performance problem based upon the choice between the if statement and the switch command. Unless you do see a performance problem you should always choose the option that makes the code more readable and maintainable.

23 March 2007