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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
using System; namespace TestApplication { class Program { static void Main(string[] args) { DateTime end; DateTime start = DateTime.Now; Console.WriteLine("### Overall Start Time: " + start.ToLongTimeString()); Console.WriteLine(); //Which is faster? //if-else? //switch? // ? : TestWhichConditionalIsFaster(10000000); TestWhichConditionalIsFaster(100000); TestWhichConditionalIsFaster(1000); end = DateTime.Now; Console.WriteLine(); Console.WriteLine("### Overall End Time: " + end.ToLongTimeString()); Console.WriteLine("### Overall Run Time: " + (end - start)); Console.WriteLine(); Console.WriteLine("Hit Enter to Exit"); Console.ReadLine(); } //what is faster? //if - else? //switch? // ? : static void TestWhichConditionalIsFaster(int NumberOfCycles) { Console.WriteLine("######## " + System.Reflection.MethodBase.GetCurrentMethod().Name); Console.WriteLine("Number of cycles: " + NumberOfCycles.ToString("#,##0")); Console.WriteLine(); TimeSpan ts; long total = 0; DateTime end; DateTime start; Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting if-else... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { if (x % 3 == 0) total += 1; else total += 0; } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting switch statement... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { switch (x % 3) { case 0: total += 1; break; default: total += 0; break; } } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting ? : shorthand... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { total += (x % 3 == 0 ? 1 : 0); } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting if-if-if-else... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { if (x % 3 == 0) total += 3; else if (x % 3 == 1) total += 2; else if (x % 3 == 2) total += 1; else total += 0; } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting 3 case switch statement... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { switch (x % 3) { case 0: total += 3; break; case 1: total += 2; break; case 2: total += 1; break; default: total += 0; break; } } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting 3 ? : shorthand... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { total += (x % 3 == 0 ? 3 : (x % 3 == 1 ? 2 : (x % 3 == 2 ? 1 : 0))); } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting 2 level if-if-if-else... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { for (int y = 0; y < (int)(NumberOfCycles / 100); y++) { if (x % 3 == 0) if (y % 3 == 0) total += 3; else if (y % 3 == 1) total += 2; else if (y % 3 == 2) total += 1; else total += 0; else if (x % 3 == 1) if (y % 3 == 0) total += 3; else if (y % 3 == 1) total += 2; else if (y % 3 == 2) total += 1; else total += 0; else if (x % 3 == 2) if (y % 3 == 0) total += 3; else if (y % 3 == 1) total += 2; else if (y % 3 == 2) total += 1; else total += 0; else if (y % 3 == 0) total += 3; else if (y % 3 == 1) total += 2; else if (y % 3 == 2) total += 1; else total += 0; } } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting 2 level 3 case switch statement... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { for (int y = 0; y < (int)(NumberOfCycles / 100); y++) { switch (x % 3) { case 0: switch (y % 3) { case 0: total += 3; break; case 1: total += 2; break; case 2: total += 1; break; default: total += 0; break; } break; case 1: switch (y % 3) { case 0: total += 3; break; case 1: total += 2; break; case 2: total += 1; break; default: total += 0; break; } break; case 2: switch (y % 3) { case 0: total += 3; break; case 1: total += 2; break; case 2: total += 1; break; default: total += 0; break; } break; default: switch (y % 3) { case 0: total += 3; break; case 1: total += 2; break; case 2: total += 1; break; default: total += 0; break; } break; } } } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting 3 level ? : shorthand... "); start = DateTime.Now; for (int x = 0; x < NumberOfCycles; x++) { for (int y = 0; y < (int)(NumberOfCycles / 100); y++) { total += (x % 3 == 0 ? (y % 3 == 0 ? 3 : (y % 3 == 1 ? 2 : (y % 3 == 2 ? 1 : 0))) : (x % 3 == 1 ? (y % 3 == 0 ? 3 : (y % 3 == 1 ? 2 : (y % 3 == 2 ? 1 : 0))) : (x % 3 == 2 ? (y % 3 == 0 ? 3 : (y % 3 == 1 ? 2 : (y % 3 == 2 ? 1 : 0))) : 0) ) ); } } end = DateTime.Now; ts = end - start; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total: " + total.ToString("#,##0") + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); GC.Collect(); } //TestWhichConditionalIsFaster } } |