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 DescriptionPurposeThis 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 FunctionalityIt 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 me 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.
TimingThe timing of the tests was controlled automatically using the Stopwatch class. Each test was performed repeatedly and the mean average of the results calculated.
Code
Click this link to download the code for the class used to perform the tests.
Test ConditionsHardware
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.
Matching Non-Matching
Empty Loop 27.3s 5.0s
Switch Statement 43.0s 5.0s
If Statement 48.0s 5.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.
Matching Non-Matching
Switch Statement 15.7s 0.0s
If Statement 20.7s 0.1s
ConclusionThe 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 re-order the testing in such a way as to provide the fastest execution.