Pergunta

I understand the difference below (at least for Java):

if( true || false ) // short-circuiting boolean operator
if( true | false )  // non-short-circuiting boolean operator

But my question is, is there any reason to use the non-short-circuiting operator when you are dealing with boolean expressions? Is there some performance benefit or use that wouldn't be considered bad practise?

Foi útil?

Solução

One reason you might want to use the non-short-circuiting operator is if you are somehow depending on side-effects of functions. For example.

boolean isBig(String text) {
  System.out.println(text);
  return text.length() > 10;
}

...
if( isBig(string1) || isBig(string2) ){
   ...
}

If you don't care about whether the println is executed then you should use the short circuit operations as above. However, if you want both strings to be printed always (thus depending on side effects) then you need to use the non-short-circuit operator.

Practically speaking, you almost always want to use the short-circuit operators. Relying on side effects in expressions is usually bad programming practice.

One exception is in very low level or performance-sensitive code. The short-circuiting operators can be slightly slower because they cause branching in the program execution. Also using bitwise operators allows you to do 32 or 64 parallel boolean operations as a single integer operation, which is very fast.

Outras dicas

If your code is performance sensitive enough and the operations cheap enough, using the non-short circuit can be faster. This is because using || involves performing a branch, and a branch prediction miss can be very expensive. Where as the | performs a calculation and examining a variable can be much faster, avoiding a branch prediction miss.

Note: this is a micro-optimisation that you will rarely see a difference unless it is called many, many times.

my question is more focused on finding a benefit to using | when only dealing with booleans

Consider the following case

while ( !credentialvalid() | (loginAttempts++ < MAX) ) {

    // tell something to user.

}

In this case | needed because I have to increase the attempt count too :)

short-circuit, meaning they don't evaluate the right hand side if it that doesn't necessary. As an example if && left hand side is false no need to evaluate right hand side one. In other way || if left is true no need to evaluate right hand side one.

non-short evaluvate both side always.

Then obviously there is a benefit with short-circuit operators.

And benefit of non-short, can find an answer here.Are there good uses for non-short-circuiting logical (boolean) operators in Java/Scala?

Consider this example too

  while (status){ // status is boolean value
          if(!a | result){// a and result are boolean value
              result=getResult(); // result can change time to time
          } 
      }

we need to check both side now.

only place where you shouldn't use non short circuit operator is when you want second statement to be executed, which shouldn't be case in conditional statements generally

No, there is no performance with non short circuit, But there is absolutely benefit with short circuit

The third statement

if( 10 | 11 )       // bitwise operator

will create a compilation error. Only boolean values can be put inside an if statement.

If you are using an IDE like Eclipse, it will automatically show the second expression i.e. after || as dead code for short-circuit boolean operator.

For simple Boolean expressions | is sometimes faster than ||. And sometimes you WANT expressions to be evaluated uniformly (as when there's a ++ in an index value.

You can't accidentally "mix" Boolean | and bitwise | since you can't mix Boolean and integer operands. But you can (of course) accidentally use | where you meant to use ||. Just be thankful it's not C, where you can accidentally use = instead of ==.

If you use,

  if(true || /*someStatementHere*/)

then the whole if block will be true because first condition is true so it does'nt have to check for the other

Simply saying, right hand side operand does not gets evaluated if the first condition gives the result in case of short circuit operators

I dont know why all are saying bitwise can operates on boolean operands only. I am telling you bitwise operator works on type of operands and return the same type of value as per operands. Just for the understanding think bitwise operator as mathematical '+' operator which can add 2 numbers. same in case of bitwise.

if(10 | 11) will not give compilation error. if it results into 0 then it will be false else gives true.

BITWISE OPERATORS are not short circuited because bitwise always needs 2 operands to get executes . Be Careful bitwise uses OPERANDS not Boolean_CONDITIONs

SYNTAX:

   for bitwise :  (operand_1) bitwise_operator (operand_2)
                 for example: ( 2 ) | ( 3 )

   for LOGICAL :  (Boolean_condition_1) LOGICAL_operator (Boolean_condition_2)
                 for example: ( true ) || ( false )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top