5

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

Spread the love

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 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:

 


Spread the love

David Lozinski