Raised This Month: $ Target: $400
 0% 

Nested if vs &&


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
zeroibis
Veteran Member
Join Date: Jun 2007
Old 08-03-2011 , 14:29   Nested if vs &&
Reply With Quote #1

Which is better for performance?

is it better to have:

Code:
if(condition1 && condition2)
{

}
or

Code:
if(condition1)
{
if(condition2)
{

}
}
Also is this still the case if doing so requires extra variable creation? Or in some cases even work preformed to the variable before check? (imagine if we also wanted to add something to value first or if we were trying to get an event string)

For example:
Code:
if(condition1)
{
new value=somemethod(var);
if(value<10)
{

}
}
OR

Code:
new value=somemethod(var);
if(condition1 && value<10)
{

}
So what do we do in order to ensure best performance? Thanks for the help
__________________
zeroibis is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 08-03-2011 , 15:01   Re: Nested if vs &&
Reply With Quote #2

In the first scenario there isn't really an important difference, besides the second one probably results in a bit more bytecode.

In The second scenario the first one can be slightly faster (depending on how much CPU time somemethod() needs) because the function will only be called when condition1 is true. But in most cases this also shouldn't make too much difference.

There are things that can take much much more CPU time, like calling FindConVar() every frame
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 08-03-2011 , 15:06   Re: Nested if vs &&
Reply With Quote #3

Ok cool,

I also assume that I should be going in order of least costly check to most costly when using && under the assumption that once something returns false the operation will stop and no more of the remaining && will be evaluated.

I have a great deal of plugins preforming a lot of if statements within hurt events so I really want to make sure that the functions are as optimized as possible.
__________________
zeroibis is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 08-03-2011 , 15:14   Re: Nested if vs &&
Reply With Quote #4

Quote:
Originally Posted by zeroibis View Post
Ok cool,

I also assume that I should be going in order of least costly check to most costly when using && under the assumption that once something returns false the operation will stop and no more of the remaining && will be evaluated.
That is correct. That's why most languages support short-circuiting operators.

You also see them used a lot when you need to check if a value is valid before using it. For instance:

PHP Code:
if (g_Cvar_SomeCvar != INVALID_HANDLE && GetConVarBool(g_Cvar_SomeCvar))
{

__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 08-03-2011 , 15:15   Re: Nested if vs &&
Reply With Quote #5

Thanks, and great example!
__________________
zeroibis is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:55.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode