Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / if/and versus nested ifs

Author
Message
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 19th Feb 2008 06:21
on a conditional statement, does it register as a fail as soon as any parameter fails or does it continue to check each one?

say I've got a line like:



if the variable is 0 then the parameter is false... will it then skip to the endif or will it call the function?

if the function still gets called then would it not be less of an impact on performance to set it up like this and eliminate the call if the variable check is already a fail?



Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 19th Feb 2008 06:36
Quote: "on a conditional statement, does it register as a fail as soon as any parameter fails or does it continue to check each one?"


No. So it'll call FunctionB() every time

Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 19th Feb 2008 07:23
thanks

Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 20th Feb 2008 05:58 Edited at: 20th Feb 2008 05:59
also,

Quote: "if the function still gets called then would it not be less of an impact on performance to set it up like this and eliminate the call if the variable check is already a fail? "


Sorry, missed this bit.

Yes your assumption is correct! If performance matters, it is better to nest such expressions. Programmers can do this by placing the expression that is the least likely and least expensive to evaluate, on the outer most if / endif nests. Which is what you've done in example above.

A general rule (to anybody reading) is the function calling is more expensive (slower) than variable/array level operations.

It should be noted though, that such optimizations generally only make a significant performance improvement, when the cost of the expression is very high, the repetition level is very high or both.

So there's no need to rush out and stream line everything. Just where it matters!

Login to post a reply

Server time is: 2025-06-04 01:50:31
Your offset time is: 2025-06-04 01:50:31