Apr 152011
 
While writing a large series of in-line if / else statements I came across a very fundamental question.  Which one is faster, in-line if / else statements or your standard if / else statement block?  No matter who I asked no one seemed to have a good answer, so I took it upon myself to find one.We can all probably agree that in-line if statements certainly take up less space and some may argue that the are much cleaner code.  And to be fair the more I’ve used in-line statements the more I’ve come to like them.  But the question still remained, in the realm of performance which one was faster.

So I decided to test it myself and to do my test I wrote a basic console app that would keep track of how long it took to execute consecutive if / else if statements in a standard block and then in-line.

The first test case I used was a simple math equation to determine if the value was even or odd.  I ran through the application 8 times first using a standard if / else statement block and recorded my results.  Then I modified the code and replaced the normal statement block with an in-line statement and ran the application another 8 times.

The following are my results and the code used, all times are in milliseconds:

Standard If Block
3845.2199, 4122.2358, 4189.2396, 2666.1525, 3759.215, 4040.2311, 2184.1249, 3958.2264

static void Main(string[] args)
{
    DateTime dtStart = DateTime.Now;
    for (int i = 0; i < Int16.MaxValue; i++)
    {
        int Remainder;
        Math.DivRem(i, 2, out Remainder);
        if (Remainder > 0)
            Console.WriteLine("Odd");
        else
            Console.WriteLine("Even");
    }
    TimeSpan tsDuration = (DateTime.Now - dtStart);
    Console.WriteLine(tsDuration.TotalMilliseconds.ToString());
    Console.ReadKey();
}

In-line If Block
4437.2538, 1890.1081, 2824.1616, 3628.2075, 3169.1812, 2745.2142, 2180.1247, 2149.123

static void Main(string[] args)
{
    DateTime dtStart = DateTime.Now;
    for (int i = 0; i < Int16.MaxValue; i++)
    {
        int Remainder;
        Math.DivRem(i, 2, out Remainder);
        Console.WriteLine((Remainder > 0 ? "Odd" : "Even"));
    }
    TimeSpan tsDuration = (DateTime.Now - dtStart);
    Console.WriteLine(tsDuration.TotalMilliseconds.ToString());
    Console.ReadKey();
}

As you can see the only consistency is that they are very inconsistent.  However, the standard if block averages out to be 3595.58 milliseconds. While, the in-line if statement averages out to 2877.92 making it faster.

Next I decided to try a standard if / else if / else statement to mix it up a bit.  The following are my results and code, again all times are in milliseconds.

Standard If / Else If Block
2841.1625, 4112.2352, 4367.2498, 2451.1402, 3063.1752, 3928.2247, 3220.184, 4052.2318

static void Main(string[] args)
{
    DateTime dtStart = DateTime.Now;
    for (int i = 0; i < Int16.MaxValue; i++)
    {
        int Remainder;
        Math.DivRem(i, 3, out Remainder);
        if (Remainder == 0)
            Console.WriteLine("Remainder 0");
        else if (Remainder == 1)
            Console.WriteLine("Remainder 1");
        else
            Console.WriteLine("Remainder 2");
    }
    TimeSpan tsDuration = (DateTime.Now - dtStart);
    Console.WriteLine(tsDuration.TotalMilliseconds.ToString());
    Console.ReadKey();
}

In-line If / Else If Block
2317.1325, 4169.2385, 4242.2426, 1964.1124, 4384.2507, 3329.1904, 3561.2037, 3339.191

static void Main(string[] args)
{
    DateTime dtStart = DateTime.Now;
    for (int i = 0; i < Int16.MaxValue; i++)
    {
        int Remainder;
        Math.DivRem(i, 3, out Remainder);
        Console.WriteLine((Remainder == 0 ? "Remainder 0" : (Remainder == 1) ? "Remainder 1" : "Remainder 2"));
    }
    TimeSpan tsDuration = (DateTime.Now - dtStart);
    Console.WriteLine(tsDuration.TotalMilliseconds.ToString());
    Console.ReadKey();
}

This time the averages were much closer with the standard if else if block coming in at 3504.45 and then the in-line if statement averaging out to 3413.32.So as you can see for a standard if else statement in-line statements seem to blow your standard statements out of the water, but when it comes to if / else if statements you are not only better of writing a standard if / else if statement block for ease of readability but performance gain as well.

  2 Responses to “In-line If Else Statements Performance Vs Standard If Else Statement Blocks”

  1. Great Research! I’ve always wondered this myself.

  2. Good one.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.