After I posted my first video about switch and if statement performance, I received some constructive criticism suggesting I may have oversimplified the problem.

They’re right. I did. I presented a trivial case that was going to perform about the same regardless of the scenario.

I decided to try this experiment again. This time, I generated a massive set of if/else statements and switch statements. There would be 200 conditions to check.

Check out the code here.

The results? Really cool. As suggested after a certain threshold the compiler uses a hashmap lookup for strings when dealing with switch. What I didn’t expect though was that there is still some string equality checks in the switch version. It’s neat to see though.

What about actual run times? I decided to benchmark and see what was going on. I set up 6 scenarios.

  1. Switch on a string value
  2. If-else if using string value
  3. If, if, if using string value (no else-if)
  4. Switch on an int
  5. If-else if on an int
  6. If, if, if on an int (no else-if)

Here’s the benchmark results.

Input: Garbage

MethodMeanErrorStdDev
Test_PrintResponse_String_Switch8.596 ns0.1456 ns0.1291 ns
Test_PrintResponse_String_If861.413 ns11.1199 ns9.8575 ns
Test_PrintResponse_String_IfOnly863.404 ns12.6337 ns11.1994 ns
Test_PrintResponse_Int_Switch2.065 ns0.0645 ns0.0604 ns
Test_PrintResponse_Int_If186.078 ns3.7522 ns4.4667 ns
Test_PrintResponse_Int_IfOnly176.614 ns3.4908 ns3.2653 ns

I picked a value that was outside the domain of matches (either 201 or “201”). In this case, if/if_only performed much worse. The switch used a hashmap and the if version used a lot of branches.

But what if the value is in the domain? I used 198.

Input: Matches

MethodMeanErrorStdDev
Test_PrintResponse_String_Switch10.975 ns0.1283 ns0.1138 ns
Test_PrintResponse_String_If1,352.415 ns15.9431 ns14.9132 ns
Test_PrintResponse_String_IfOnly1,346.991 ns26.7235 ns24.9972 ns
Test_PrintResponse_Int_Switch2.886 ns0.0851 ns0.0796 ns
Test_PrintResponse_Int_If171.495 ns3.1895 ns2.9834 ns
Test_PrintResponse_Int_IfOnly178.288 ns3.4112 ns3.1908 ns

The performance is worse? I’ll chalk this up to two things – measurement error and the possibility that I picked a bad example. I’m sure there are other inputs that would perform better.

I’m not claiming this is the definitive answer on if or switch being faster! I’m just saying in this particular case for this particular code, switch was faster.

Plus studying the IL is a ton of fun. Just saying!

Published by Elias Puurunen

Elias Puurunen is a versatile entrepreneur and President of Northern HCI Solutions Inc., an IT consulting firm which has worked with Fortune 500 companies, governments, and startups. He has spoken at conferences in Canada and the United States and has been published around the world. Part of his work led to an agreement between the Canadian Government and Siemens Canada, creating jobs and investment into green infrastructure. His company's event management app, the Tractus Event Passport connects people at conferences, seminars and symposiums across Canada. Today he is a consultant and advisor to technology firms and government organizations. He lectures at the University of Waterloo on Coding for Policy Analysis for the School of Public Policy. He is the author of Beyond Passwords: Secure Your Business, a cyber-security book for small business owners.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: