The text of this article is not in this database — only its details are. The old site it was published on is gone, but the Internet Archive kept a copy: Performance of If-else if tree vs. Switch (multiple variables) in C#
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

FireMystPosted Feb 6, 2014, 1:44 AM
This is a good article. I like how you laid out the code, It would be better too if you did benchmark tests against all three constructs like this blog: http://cc.davelozinski.com/c-sharp/what-is-the-fastest-conditional-statement I've personally found the nested switch/case statements slower than their if/else equivalents.
lgsalamonPosted Oct 7, 2010, 10:52 AM
try this : static void newswitchtest() { Stopwatch timer; timer = Stopwatch.StartNew(); timer.Stop(); int bin; timer.Start(); for (int i = 0; i < 100000000; ++i) { for (int a = 0; a < 2; ++a) { for (int b = 0; b < 2; ++b) { for (int c = 0; c < 2; ++c) { bin = a + (b << 1) + (c << 2); switch (bin) { case 0: break; case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; } } } } } timer.Stop(); Console.WriteLine(timer.ElapsedMilliseconds.ToString()); } My tests : 9704 ms if/else 8251 ms switch 5817 ms the code above Good article.
Sam HobbsPosted Sep 28, 2010, 5:56 PM
Another possibility would be to create an array of methods using a delegate type and then instead of using a switch, just index into the array. That could be quite efficient if there are more than 3 conditions, right?