5

C# .Net: What is the fastest conditional statement?

C# .Net: What is the fastest conditional statement?

This will benchmark the C# if-else construct, switch statement, and C# ternary operator (?) to determine which is the fastest in C# .Net.

 

Most applications have at least one type of conditional statement in its underlying code, whether it’s the if-else construct, switch statement, or the inline conditional operator (?).

 

Typically there could be hundreds, or even thousands of these, in the code. So that’s when this curious consultant started wondering in C# .Net: What is the fastest conditional statement? If/Else? Switch statement? Or the Ternary Operator?

 

The Set Up:

I wrote a C# Console application to test them.

 

The code is written in Visual Studio 2013 targeting .Net Framework version 4.5 x64. The source code is available at the end of this blog so you can benchmark it on your own system if you wish.

 

In a nutshell, here are the methods:

#

Technique

Code Snippet

T1

Single if-else

T2

Single level switch

T3

Single ? operator

T4

If-else if-else if-else

T5

3-case switch statement

T6

3 inline conditionals ?

T7

2 level if-else if-else if-else

T8

2 level 3 case switch statements

T9

2 level 3 inline conditionals

 

T1, T2, T3 represent one test; T4, T5, T6 is another group test; T7, T8, T9 is the last group test.

 

The exe file was installed and run on an Alienware M17X R3 running Windows 7 64-bit with 16 GB memory on an i7-2820QM processor.

The test was run for each of the 3 equivalent techniques over 10,000,000 iterations, 100,000 iterations, and 1,000 iterations.

 

Ready! Set! Go!

Before starting, my gut instinct told me the if-else construct was going to be the fastest.

 

Let’s see what happened.

 

All times are indicated in hours:minutes:seconds.milliseconds format. Lower numbers indicate faster performance.

Winner(s) marked in green. As was said in Top Gun, there’s no points for second place:

 

@ 10,000,000

@ 100,000

@ 1,000

T1: single if-else

00:00:00.0160009

00:00:00

00:00:00

T2: single level switch

00:00:00.0160009

00:00:00

00:00:00

T3: single ? operator

00:00:00.0240014

00:00:00

00:00:00

 

T4: If-else if-else if-else

00:00:00.0210012

00:00:00.0010000

00:00:00

T5: 3-case switch statement

00:00:00.0250014

00:00:00

00:00:00

T6: 3 inline conditionals ?

00:00:00.0210012

00:00:00

00:00:00

 

T7: 2 level if-else if-else if-else

01:02:25.5852352

00:00:00.3780216

00:00:00

T8: 2 level 3 case switch statements

02:35:09.6934840

00:00:00.9310533

00:00:00.0010000

T9: 2 level 3 inline conditionals

01:06:53.9065824

00:00:00.3930225

00:00:00

 

Only One Real Surprise in the Results

For the most part, the if-else construct won but only by fractions upon fractions of a millisecond. Even then, it was only when there were more than 100,000 or more consecutive statement executions.

 

The only real surprise is the 2+ hours it took the 2-level switch case statement to complete. Why did it take more than twice as long as the if-else and equivalent conditional (?) construct?  That construct performed relatively poorly across the board.

 

The coding lesson learned?

On my system, unless someone spots a flaw in my test code, as a C# .Net programmer you should stay away from nested switch-case statements as they perform relatively poorly compared to everything else. Single level switch statements are fine though.

 

Obviously results may vary, and you should test on your system before micro-optimizing this functionality in your C# .Net application.

 

Hence why I’m giving you the code below. 🙂

 

The Code:

 

 

(Visited 640 times, 1 visits today)
  • Edhill

    It is interesting, but you should expand the tests – specifically most compilers optimize switches differently depending on the number of cases, with switches having some theoretical advantage with more cases, but also potentially screws up the cpu’s cache and branch-predictor more… so it will likely depend on both hardware and compiler version used. (Just dropping some info for the interested.)

  • Tomáš Gryžboň

    Shouldn’t be 1-level deep switch faster than 1-level deep if statement ?

    • FireMystdl

      Why do you ask? What makes you think it should?

      • Tomáš Gryžboň

        Because the compiler can build jump tables where applicable. For example, when you use the reflector to look at the code produced, you will see that for huge switches on strings, the compiler will actually generate code that uses a hash table to dispatch these. The hash table uses the strings as keys and delegates to the case codes as values.This has asymptotic better runtime than lots of chained if tests and is actually faster even for relatively few strings.

        • FireMystdl

          The compiler “can”, but doesn’t mean it “will”. The test code above doesn’t have a huge amount of switches either… so an if statement could hypothetically run faster if the condition is met in the first or second test. Because I’m doing a mod %, the test code is going to run through all the cases, thus the “if” statement will only have to do the maximum comparison a few times in the overall grand scheme of things.

          The code is supplied in the blog. Take a copy, run it on your machine, and see which comes out faster for you. Let us know how you go.