Click here to Skip to main content
15,893,487 members
Articles / Programming Languages / C#
Tip/Trick

.Net Performance tip - 3

Rate me:
Please Sign up or sign in to vote.
3.60/5 (8 votes)
22 May 2010CPOL 16.1K   1   8
.Net Performance tip - 3
.Net Performance tip - 3

Last week i was trying to explore how the if statement performs under various conditions as shown below.

if( statement 1)
{
 if( statement 2)
 {
  if( statement 3)
  {
   ......
  }
 }
}


vs

if( statement 1 && statement 2 && statement 3)
{
  .....
}



The results indicate that, the second approach is far better than the first one.

Hence, the recommendation would be to use the second approach over the first one

I hope this helps!.

Regards,
-Vinayak

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect MindTree Ltd
India India
Motivated achiever who guides organizations in applying technology to business settings, provides added value, and creates project deliverables in a timely manner. An experienced Technical Consultant, have successfully led large project teams of more than 20 people from requirements gathering to implementation and support using C#, .NET ,ADO.NET, ADO.NET Entity Framework,ASP.NET,ASP.NET MVC, WCF and SQL Server.

Comments and Discussions

 
Questionmy vote 2+ Pin
joginder-banger26-Dec-14 6:48
professionaljoginder-banger26-Dec-14 6:48 
GeneralReason for my vote of 2 it depends on the outcome of the eva... Pin
johannesnestler25-May-10 5:06
johannesnestler25-May-10 5:06 
GeneralReason for my vote of 1 useless without numbers Pin
SledgeHammer0122-May-10 8:08
SledgeHammer0122-May-10 8:08 
GeneralReason for my vote of 2 See my comments....... Pin
DaveAuld22-May-10 2:55
professionalDaveAuld22-May-10 2:55 
GeneralIt would be of benefit if you provided code on how you perfo... Pin
DaveAuld22-May-10 2:29
professionalDaveAuld22-May-10 2:29 
GeneralThis is not always true! Pin
DaveAuld22-May-10 2:54
professionalDaveAuld22-May-10 2:54 
In the second instance, Statement1, Statement2 and Statement3, must be evaluated before the if can do the && operation. If Statement1, Statement2 or Statement3 are complex lengthy operations, then he whole function would suffer.

In this scenario nested if's would be quicker, providing you optimise the code to ensure the quickest executing evaluation is placed first.


.Net4 also introduces the AndAlso Class (in Systems.Activities.Expressions) which provides the same functionality of the VB.Net AndAlso operator which short circuits the evaluation as soon as one of them returns false.
Dave

Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.com


GeneralRe: This is not always true! Pin
Vinayaka Krishna Shenoy22-May-10 5:34
Vinayaka Krishna Shenoy22-May-10 5:34 
GeneralRe: This is not always true! Pin
DaveAuld22-May-10 6:37
professionalDaveAuld22-May-10 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.