And vs. AndAlso, performance??

A

A Traveler

I was just curious if anyone knows how the combinations of And/AndAlso and
Or/OrElse compare in terms of performance.
Which takes more work for the system, performing two evaluations on an And
or performing short-circuiting on an AndAlso?

Purely for enlightenment.

Thanks in advance.

- Arthur Dent.
 
H

Herfried K. Wagner [MVP]

A Traveler said:
I was just curious if anyone knows how the combinations of And/
AndAlso and Or/OrElse compare in terms of performance.
Which takes more work for the system, performing two evaluations on an And
or performing short-circuiting on an AndAlso?

I would prefer 'AndAlso'/'OrElse' when a logical operation should be done.
Short circuiting is faster in many cases, because often parts of the
expression don't need to be evaluated.
 
I

Imran Koradia

Well - both OrElse and AndAlso are short circuit operators. So for OrElse,
if the first condition is met, it won't even check the second one (which
could be a lengthy operation). Similarly, for AndAlso, if the first
condition is not met, it'll break out without even checking the second one.
So in both the short circuit operations, you're saving on the extra
condition check when the first one is satisfied (OrElse) or not satisfied
(AndAlso). So these should give you better performance in general. Note that
its the expressions that are being evaluated that take the processing time
and not the operations performed by the operators themselves (which are
simply boolean operations). Its just the way in which the short-circuit
operators evaluate which gives you a higher probability of better
performance.


hope that helps..
Imran.
 
A

A Traveler

I dont think i was clear in my question...

I know _both_ OrElse and AndAlso are short-circuit. But take a case where
the time to process the individual conditions is negligible and can be
discounted. Then assume that you will break out on the short-circuit only on
avg about half the time (or less). Will the Also/Else save time in the long
run then?
Im thinking along the lines of database indexes. By basic principles, an
index is a good thing that improves response time when used on a commonly
search field, just like short-circuit logic is a faster op in basic
principle and runs faster. But now, if your db index will only actually hit
about 50% of the time (or less), it essentially becomes useless (and even a
hindrance, for the extra work the db does to maintain it).
Im wondering if the Also/Else operators are affected by any sort of similar
phenomenon, where the % of hits (or some other factor) may degrade the
benefit ofr them and possibly even make it so they are actually slower. This
would, i imagine, have a great deal to do with how they are actually
implemented in the CLR.

CheerZ.
 
K

Ken Halter

A said:
I dont think i was clear in my question...

I know _both_ OrElse and AndAlso are short-circuit. But take a case where
the time to process the individual conditions is negligible and can be
discounted. Then assume that you will break out on the short-circuit only on
avg about half the time (or less). Will the Also/Else save time in the long
run then?

As an innocent bystander just browsing this group, I have to add...

"time to process the individual conditions is negligible"

If that's the case, then there's no benefit to short circuits. When the
tests do take considerable time, it makes sense. The code below will
obviously benefit from a short circuit.

If ResultsFromHardDriveSearch(SomeSearchTerm) > 0 And
ResultsFromInternetSearch(SomeSearchTerm) > 0 Then

On the other hand, the code below won't benefit much.

If X = 1 Or X = 2 Then

fwiw, you can short circuit VB6 by replacing If/Then's with Select Case's
 
A

A Traveler

Yes, i am aware that if the conditions are negligible, it doesnt make
*practical* sense to bother with short circuiting.
I am not looking for a practical answer to a specific problem though. I am
simply asking whether there are certain conditions (such as hit percentages)
under which an And performs better than AndAlso, due simply to the way _And_
is implemented vs. how _AndAlso_ is implemented. And i dont mean for a
single run, but over the course of a large number of runs, where sometimes
it will short circuit and sometimes it wont.
 
J

Jay B. Harlow [MVP - Outlook]

Arthur,
Im wondering if the Also/Else operators are affected by any sort of
similar phenomenon, where the % of hits (or some other factor) may degrade
the
I am certain there are cases where this is true, I would expect reversing
the order of the operands then improve performance?

In other words if the first line below performs poorly then use the second
line:

If b1 AndAlso b2

If b2 AndAlso b1


Just as you use a tool, such as SQL Server's Query Analyzer to decide if
your DB index is useful or not I would recommend you use a tool, such as CLR
Profiler to determine if And/Or verses AndAlso/OrElse is useful in a
specific routine.

Info on the CLR Profiler:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto13.asp

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Arthur,
In addition to my other comments, you can use ILDASM.EXE to look at the IL
created.

//000131: Dim b1 As Boolean
//000132: Dim b2 As Boolean
//000133: If b1 And b2 Then
IL_0013: ldloc.0
IL_0014: ldloc.1
IL_0015: and
IL_0016: brfalse.s IL_0018
//000134:
//000135: End If
IL_0018: nop
//000136: If b1 AndAlso b2 Then
IL_0019: ldloc.0
IL_001a: brfalse.s IL_001f
IL_001c: ldloc.1
IL_001d: brfalse.s IL_001f
//000137:
//000138: End If
IL_001f: nop

The other thing to consider is what will the JIT do to the IL when it
creates the machine executable code.

IMHO this question really falls into the 80/20 rule category. That is 80% of
the execution time of your program is spent in 20% of your code. I will
optimize (worry about performance, memory consumption) the 20% once that 20%
has been identified & proven to be a performance problem via profiling (CLR
Profiler is one profiling tool).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware/yetOptimization.pdf


Hope this helps
Jay


A Traveler said:
Yes, i am aware that if the conditions are negligible, it doesnt make
*practical* sense to bother with short circuiting.
I am not looking for a practical answer to a specific problem though. I am
simply asking whether there are certain conditions (such as hit
percentages) under which an And performs better than AndAlso, due simply
to the way _And_ is implemented vs. how _AndAlso_ is implemented. And i
dont mean for a single run, but over the course of a large number of runs,
where sometimes it will short circuit and sometimes it wont.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top