If-Else-If Vs. Switch

Introduction

There are two methods available in Microsoft .NET framework and C# language for conditional processing where discrete values can be selected from. The Switch statement is more considered to be more efficient than if-else-if ladder, but less flexible.

Purpose

This test is designed to check and compare the time taken for execution by switch statement and if-else-if ladder code when they are used to choose a match from discrete series of values. Much more flexibility is provided by if-else-if ladder than switch statement when it is used in the test, but the purpose is to determine which is taking less time, ideally we should not consider this.

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.

Hardware

The test results included below are those produced using an Inspiron 15r 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

The tests were executed using one operating systems, with the latest service packs and patches. That is:

  • Windows 7 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 2010 Team Edition
  • Compiled for 32-bit or 64-bit processors using Visual Studio 2010 Team Edition

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 13.9s 8.6s
Switch Statement 22.0s 9.3s
If Statement 39.8s 50.9s

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

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 optimize 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.

Tagged with: , ,