Quantcast

If/else-statement Performance Optimization

Get the WebProNews Newsletter:
[ Business]

Have you ever considered how well a regular if/else-statement performs? I haven’t, but after watching this Channel9 video I wanted to test it.

In the video, Brian Beckman explains that the order of the statements is important to the performance. In other words; the most likely statement to succeed should be the first. Let’s take a look at a simple method with an if/else statement. As you can see, it doesn’t do much, but that’s exactly what we want in order to test the if/else-statement.

   private bool RunIf(string input)
   {
     if (input == "hello")
       return true;
     else if (input == "jelly")
       return true;
     else
       return true;
   }

If the theory is correct, it should be faster to pass in “hello” than “jelly”, which again would be faster than a random third string. To test this, we need a method that calls RunIf() a lot of times in order to measure it.

   private void Test()
   {
     DateTime start = DateTime.Now;

     for (int i = 0; i < 100000000; i++)
     {
       RunIf("hello");
      }

     TimeSpan span = DateTime.Now.Subtract(start);
     Console.Write(span.TotalMilliseconds);
   }

Results

I did three tests. In the first I passed "hello" to the RunIf, the second "jelly" and the third "other" - all 5 letter words for consistency. Here's the total runtime in milliseconds for each of the three tests.

1. "hello": 1546 milliseconds

2. "jelly": 3687 milliseconds

3. "other": 4453 milliseconds

It is actually more than twice as fast to run the first if-statement as having to move into the "if else" statement.

So, what can we use this information for? Probably not much, because I had to make a hundred million iterations in the Test() method just to be able to measure the result. Unless of course you have an application that does just that, then this is merely a little information for your consideration.

Or maybe we can go as far as to say that it would improve the performance of your if/else-statements to run the first if-statement more often than the else statement.

Comments

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

If/else-statement Performance Optimization
About Mads Kristensen
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/ WebProNews Writer
Top Rated White Papers and Resources
  • Kalicharan

    Is the performance of the below code is same as of yours? Or is there any difference in decision making?

    private bool RunIf(string input)
    {
    if (input == “hello”)
    return true;
    else
    {
    if (input == “jelly”)
    return true;
    else
    return true;
    }
    }