JNF
JNF

Reputation: 3730

C# elseif vs. else{if} speed

Of the following, which is faster?

if {...}
else if{...}

or

if {...}
else {if{...}}

Is else if compiled to one statement or into the same logic as the second?

Upvotes: 1

Views: 728

Answers (5)

Fabio
Fabio

Reputation: 3120

I wrote these methods in a C# console application, and I analysed the IL of the resulting assembly using Telerik JustDecompile.

private int Method1(int x)
{
    int y = 0;
    if (x == 0)
    {
        y = 10;
    }
    else if (x == 1)
    {
        y = 20;
    }
    return y;
}

private int Method2(int x)
{
    int y = 0;
    if (x == 0)
    {
        y = 10;
    }
    else
    {
        if (x == 1)
        {
            y = 20;
        }
    }
    return y;
}

The IL result was the same for both methods.

.method private hidebysig 
    instance int32 Method1 (
        int32 x
    ) cil managed 
{
    .locals init (
        [0] int32 y
    )

    IL_0000: ldc.i4.0
    IL_0001: stloc.0
    IL_0002: ldarg.1
    IL_0003: brtrue.s IL_000a

    IL_0005: ldc.i4.s 10
    IL_0007: stloc.0
    IL_0008: br.s IL_0011

    IL_000a: ldarg.1
    IL_000b: ldc.i4.1
    IL_000c: bne.un.s IL_0011

    IL_000e: ldc.i4.s 20
    IL_0010: stloc.0

    IL_0011: ldloc.0
    IL_0012: ret
}

EDIT: In my first answer, I told I've found some extra Nop instructions in the IL code generated, but this was because I've compiled my application in Debug mode. In Release mode, there's no differente.

Upvotes: 2

Guffa
Guffa

Reputation: 700192

There is no difference in the result.

There is no elseif in C#, so an else if is just an else that contains another if.

The brackets aren't needed when the if or else is followed by a single statements, and brackets can be added around statements anywhere. You can stack seveal brackets and the result is still the same:

{
  if (something) {
    {
      {
        // do something
      }
    }
  } else {
    {
      {
        {
          if (something) {
            {
              // do something
            }
          }
        }
      }
    }
  }
}

The only difference that you can see by adding brackets is that it creates a scope, but that is only used by the compiler to determine the scope of identifiers, the compiled code is still the same.

Upvotes: 2

Microsoft DN
Microsoft DN

Reputation: 10010

Consider

if(condition1)
{
      // code
}
else
{
     if(condition2)
     {
         // code
     }
}

In above code, if condition1 is false, execution will to else. Then in else it will again check whether condition2 is true.

Now consider following code

if(condition1)
{
    // code
}
else if(condition2)
{
    // code        
}

Now in above code, if condition1 is false, control will go to if else only if condition2 is true.

So from this reference

Since only one statement is executed in IF - ELSE IF - ELSE tree. ELSE IF can provide an option to execute something else if the earlier expression is FALSE and it can be very useful because ELSE IF's statement only will executed if it's expression is evaluated to TRUE on the other hand ELSE's statement will always be executed if the IF's expression was evaluated to FALSE.

Upvotes: 0

Noone
Noone

Reputation: 425

I can't say what is the compiled code, but let see it this way :

You can have a if condition with multiple lines included inside the if

if 
{
   // multiple statements
}
else 
{
   // multiple statements
}

You can also have a if condition with single line statement inside

if 
   // one line statement
else
   // one line statement

Now, imagine that the if condition after the else is a one line statement...

if 
  ...
else 
  if ()
  {

  }

Upvotes: 1

selkathguy
selkathguy

Reputation: 1171

else if is mostly the same. If you choose to write it in the latter fashion, the compiler will produce the exact same IL code, but I believe this depends heavily on your build settings,and whether the project is built for debug or release, which optimizations are enabled, etc. In general, they should be for all intents and purposes identical, and you may treat them as such. My recommendation is that you write whichever is more legible.

Like Soner Gönül said in comments, you can always test this trivially.

Upvotes: 5

Related Questions