views:

1327

answers:

11

I have an if statment with two conditions (seperated by an OR operator), one of the conditions covers +70% of situations and takes far less time to process/execute than the second condition, so in the interests of speed i only want the second condition to be processed if the first condition evaluates to false.

if i order the conditions so that the first condition (the quicker one) appears in the if statment first - on the occasions where this condition is met and evaluates true is the second condition even processed ?

if ( (condition1) | (condition2) ){
  // do this
}

or would i need to nest two if statements to only check the second condition if the first evaluates to false ?

if (condition1){
  // do this
}else if (condition2){
  // do this
}

I am working in php, however i assume that this may be language-agnostic.

Thanks

A: 

Since this is tagged language agnostic I'll chime in. For Perl at least, the first option is sufficient, I'm not familiar with PHP. It evaluates left to right and drops out as soon as the condition is met.

dagorym
+3  A: 

Pretty much every language does a short circuit evaluation. Meaning the second condition is only evaluated if it's aboslutely necessary to. For this to work, most languages use the double pipe, ||, not the single one, |.

See http://en.wikipedia.org/wiki/Short-circuit_evaluation

Chris Lively
A: 

In most languages with decent optimization the former will work just fine.

levand
+7  A: 

For C, C++, C#, Java and other .NET languages boolean expressions are optimised so that as soon as enough is known nothing else is evaluated.

An old trick for doing obfuscated code was to use this to create if statements, such as:

a || b();

if "a" is true, "b()" would never be evaluated, so we can rewrite it into:

if(!a)
    b();

and similarly:

a && b();

would become

if(a)
    b();

Please note that this is only valid for the || and && operator. The two operators | and & is bitwise or, and and, respectively, and are therefore not "optimised".

EDIT: As mentioned by others, trying to optimise code using short circuit logic is very rarely well spent time.

First go for clarity, both because it is easier to read and understand. Also, if you try to be too clever a simple reordering of the terms could lead to wildly different behaviour without any apparent reason.

Second, go for optimisation, but only after timing and profiling. Way too many developer do premature optimisation without profiling. Most of the time it's completely useless.

Mats Fredriksson
+2  A: 

I've seen a lot of these types of questions lately--optimization to the nth degree.

I think it makes sense in certain circumstances:

  1. Computing condition 2 is not a constant time operation
  2. You are asking strictly for educational purposes--you want to know how the language works, not to save 3us.

In other cases, worrying about the "fastest" way to iterate or check a conditional is silly. Instead of writing tests which require millions of trials to see any recordable (but insignificant) difference, focus on clarity.

When someone else (could be you!) picks up this code in a month or a year, what's going to be most important is clarity.

In this case, your first example is shorter, clearer and doesn't require you to repeat yourself.

Michael Haren
+2  A: 

According to this article PHP does short circuit evaluation, which means that if the first condition is met the second is not even evaluated. It's quite easy to test also (from the article):

<?php
/* ch06ex07 – shows no output because of short circuit evaluation */

if (true || $intVal = 5) // short circuits after true
{

echo $intVal; // will be empty because the assignment never took place
}

?>
Farinha
+2  A: 
Anders Sandvig
A: 

The | is a bitwise operator in PHP. It does not mean $a OR $b, exactly. You'll want to use the double-pipe. And yes, as mentioned, PHP does short-circuit evaluation. In similar fashion, if the first condition of an && clause evaluates to false, PHP does not evaluate the rest of the clause, either.

Brian Warshaw
+1  A: 

While using short-circuiting for the purposes of optimization is often overkill, there are certainly other compelling reasons to use it. One such example (in C++) is the following:

if( pObj != NULL && *pObj == "username" ) {
    // Do something...
}

Here, short-circuiting is being relied upon to ensure that pObj has been allocated prior to dereferencing it. This is far more concise than having nested if statements.

Matt Dillard
A: 

VB.net has two wonderful expression called "OrElse" and "AndAlso"

OrElse will short circuit itself the first time it reaches a True evaluation and execute the code you desire.

If FirstName = "Luke" OrElse FirstName = "Darth" Then
   Console.Writeline "Greetings Exalted One!"
End If

AndAlso will short circuit itself the first time it a False evaluation and not evaluate the code within the block.

If FirstName = "Luke" AndAlso LastName = "Skywalker" Then
   Console.Writeline "You are the one and only."
End If

I find both of these helpful.

Dillie-O
Arda Xi
+1  A: 

The short-circuiting is not for optimization. It's main purpose is to avoid calling code that will not work, yet result in a readable test. Example:

if (i < array.size() && array[i]==foo) ...

Note that array[i] may very well get an access violation if i is out of range and crash the program. Thus this program is certainly depending on short-circuiting the evaluation!

I believe this is the reason for writing expressions this way far more often than optimization concerns.