My main preference when coding is to use the bitwise operators (such as OR, AND).
However I am also aware of the short-circuit "equivalents" (i.e. ORELSE, ANDALSO), and after some reflection, I'm not too sure why the bitwise operators would be used in their LOGICAL uses (i.e. if x > 5 and y < 8), except that they can overloaded and short-circuit operators can't. The main point to my argument is that short-circuit operations can be faster (not both operands have to be calculated, just the first one before the program/computer can determine whether to continue calculating).
Is this assumption about speed correct? Is there something about short-circuit operators which may cause issues with some logical statements? Or should we all just be using AND and OR only for the few logical statements that need it for some reason, and really we should be using ANDALSO and ORELSE?
Short-Circuit: Why Use their Bitwise Counterparts?
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Short-Circuit: Why Use their Bitwise Counterparts?
Personal preference is: short-cut operators (speed, boolean comparison).
However, can also be done without (the binary operators) but, with more code to get close, e.g.:With the short-cut operators:
However, can also be done without (the binary operators) but, with more code to get close, e.g.:
Code: Select all
If (cond1) Then
If (cond2) Then
If (cond3) Then
If (cond4) Then
...
End If
End If
End If
End If
Code: Select all
If (cond1) AndAlso (cond2) Then
If (cond3) AndAlso (cond4) Then
...
End If
End If
Last edited by MrSwiss on Jul 03, 2017 14:30, edited 1 time in total.
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Short-Circuit: Why Use their Bitwise Counterparts?
Thanks for the clarification.MrSwiss wrote:Personal preference is: short-cut operators (speed).
However, can also be done without (the binary operators) but, with more code to get close, e.g.:With the short-cut operators:Code: Select all
If (cond1) Then If (cond2) Then If (cond3) Then If (cond4) Then ... End If End If End If End If
Code: Select all
If (cond1) AndAlso (cond2) Then If (cond3) AndAlso (cond4) Then ... End If End If
Re: Short-Circuit: Why Use their Bitwise Counterparts?
Have you seen the already existing feature request: #246 Execution speed optimization of 'Andalso' and 'Orelse' ?
You can add text inside 'discussion' (need to login)!
The included test program provides the performances for 3 syntaxes used in a particular case.
In any case, I always find that the nested conditional branchings are the fastest.
You can add text inside 'discussion' (need to login)!
The included test program provides the performances for 3 syntaxes used in a particular case.
In any case, I always find that the nested conditional branchings are the fastest.
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Short-Circuit: Why Use their Bitwise Counterparts?
Thanks for the info and will see which is faster!fxm wrote:Have you seen the already existing feature request: #246 Execution speed optimization of 'Andalso' and 'Orelse' ?
You can add text inside 'discussion' (need to login)!
The included test program provides the performances for 3 syntaxes used in a particular case.
In any case, I always find that the nested conditional branchings are the fastest.
Re: Short-Circuit: Why Use their Bitwise Counterparts?
Oh, I'm glad to see that dkl optimised andalso and orelse, back in 2015. I've been avoiding those for years, due to the shocking assembly that was generated for them. They aren't much slower than and/or anymore. And these days I use -gen gcc for speed, so no longer have any reason to avoid them.
Re: Short-Circuit: Why Use their Bitwise Counterparts?
Oh! I hadn't seen there had been discussion about the speed of these operators. It's very interesting.
Now, aside from speed, I believe there are good reasons to sometimes use And/Or and sometimes AndAlso/OrElse. It is better, in my opinion, to use the latter when one knows that the expression on the right has a chance of evaluating to an error and this is acceptable. That's very clear. On the other hand, it's important to use the former whenever one believes none of the two expressions should ever evaluate to an error, so that if that assumption were wrong, one can detect it more easily. In general, I'm very fond of the short-circuit operators and I use them frequently.
Now, aside from speed, I believe there are good reasons to sometimes use And/Or and sometimes AndAlso/OrElse. It is better, in my opinion, to use the latter when one knows that the expression on the right has a chance of evaluating to an error and this is acceptable. That's very clear. On the other hand, it's important to use the former whenever one believes none of the two expressions should ever evaluate to an error, so that if that assumption were wrong, one can detect it more easily. In general, I'm very fond of the short-circuit operators and I use them frequently.
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Short-Circuit: Why Use their Bitwise Counterparts?
I use AND/OR when I mimic real logic stuff, so when I want to compute some binary integers (c = a AND b). Otherwise ANDALSO/ORELSE are more useful for conditional testing. And it's a good thing if they are fast, otherwise this would be quite sad.xlucas wrote:I believe there are good reasons to sometimes use And/Or and sometimes AndAlso/OrElse.
Example, you have a pointer P:
Code: Select all
if P<>0 andAlso *P=33 then
...
else
? "null pointer!"
end if