5

I am wondering whether nested if is better than AND statement. I have a loop that goes so many times so I am thinking of faster execution available. Below is the code that has same logic with my code. The nested if statement is inside a loop.

   for ( int i = 0; i < array.length; i++)
   {
      // do stuff
      if (x == 5)
      {
         if (y == 3)
         {
             // do stuff
         }
      }
   }  

Will my code be faster by a significant difference if I replace the nested if with this And STATEMENT?

     if ((x == 5) && (y == 3))
           // do stuff

I have read this link but I didn't find the answer. I am a student and still learning, thanks for all the feedback!

Community
  • 1
  • 1
blenzcoffee
  • 851
  • 1
  • 11
  • 35

4 Answers4

9

No, it will not have a significant difference on performance, but there may be a difference in readability.

Both of those will generate the same IL when compiled with optimization/release(Tested with LINQPad):

IL_0000:  ldc.i4.5    
IL_0001:  stloc.0     
IL_0002:  ldc.i4.s    0A 
IL_0004:  stloc.1     
IL_0005:  ldloc.0     
IL_0006:  ldc.i4.5    
IL_0007:  bne.un.s    IL_000D
IL_0009:  ldloc.1     
IL_000A:  ldc.i4.3    
IL_000B:  pop         

Even without optimization the difference is not that significant:

Nested statements:

IL_0001:  ldc.i4.5    
IL_0002:  stloc.0     
IL_0003:  ldc.i4.s    0A 
IL_0005:  stloc.1     
IL_0006:  ldloc.0     
IL_0007:  ldc.i4.5    
IL_0008:  ceq         
IL_000A:  ldc.i4.0    
IL_000B:  ceq         
IL_000D:  stloc.2     
IL_000E:  ldloc.2     
IL_000F:  brtrue.s    IL_0020
IL_0011:  nop         
IL_0012:  ldloc.1     
IL_0013:  ldc.i4.3    
IL_0014:  ceq         
IL_0016:  ldc.i4.0    
IL_0017:  ceq         
IL_0019:  stloc.2     
IL_001A:  ldloc.2     
IL_001B:  brtrue.s    IL_001F
IL_001D:  nop         
IL_001E:  nop 

Not Nested statements:

IL_0001:  ldc.i4.5    
IL_0002:  stloc.0     
IL_0003:  ldc.i4.s    0A 
IL_0005:  stloc.1     
IL_0006:  ldloc.0     
IL_0007:  ldc.i4.5    
IL_0008:  bne.un.s    IL_0013
IL_000A:  ldloc.1     
IL_000B:  ldc.i4.3    
IL_000C:  ceq         
IL_000E:  ldc.i4.0    
IL_000F:  ceq         
IL_0011:  br.s        IL_0014
IL_0013:  ldc.i4.1    
IL_0014:  nop         
IL_0015:  stloc.2     
IL_0016:  ldloc.2     
IL_0017:  brtrue.s    IL_001B
IL_0019:  nop 
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • @Almo, Having nested if-statements when you don't need to will cause everythign to be less readable, but that's subjective (maybe) :) – Filip Ekberg Jul 05 '12 at 19:30
  • 2
    I agree with you, but the phrase "but on readability it" doesn't make any sense. It's ironic since it comments on readability, and is not itself readable. :) – Almo Jul 05 '12 at 19:30
  • @Almo, Hah, guess I have to blame it on not being a native speaker! – Filip Ekberg Jul 05 '12 at 19:31
  • English might not be as tough as French to learn, but it's not easy, either. :) – Almo Jul 05 '12 at 19:33
  • They will be compiled to the same assembly (in this code), so there is no difference in all. – Saeed Amiri Jul 05 '12 at 19:34
  • Added some `IL` code to show that there's no difference when compiled with release/optmization, but there is a difference when compiled without optmization/debug. Even without optimization the difference is not significant. – Filip Ekberg Jul 05 '12 at 19:53
3

No, there won't be any difference between the two. However, the AND makes fewer lines and is more readable (if you don't have that many conditions)

There are cases where ifs are better and feel more natural, one common example is the following:

String s=//...
if(s==null)return;
if(s.Length > 0) //do something

With an AND, this can be replaced by:

if ((s!=null) && (s.Length > 0)) //Dosomething

Many developers do this mistake:

if ((s.Length > 0) && (s!=null) ) //Dosomething

Which will end up in a null reference exception

As you can see, it is more natural to think of doing the null check first when using separate ifs

GETah
  • 20,922
  • 7
  • 61
  • 103
3

.NET will stop checking if the first part of the conditional is false, so there will be no performance difference between the two.

Kyeotic
  • 19,697
  • 10
  • 71
  • 128
  • does this logic happen in AND statement too? – blenzcoffee Jul 05 '12 at 19:34
  • That is what I meant. In the and statement, if the first half is false, it wont bother checking the second half. – Kyeotic Jul 05 '12 at 19:35
  • 1
    That's called short circuiting, and all of the logical operators short circuit whenever possible, meaning if they know the result *must* be true/false they immediately stop executing any other statements since they can't affect the result. – Servy Jul 05 '12 at 19:36
1

In compiled code, there is no reason the speeds should be different, they will translate to exactly the same assembly code. I definitely agree about the readability factor, and it will shorten the length of your class as well.

Chris Knight
  • 1,448
  • 1
  • 15
  • 21