PC Review


Reply
Thread Tools Rate Thread

And vs. AndAlso, performance??

 
 
A Traveler
Guest
Posts: n/a
 
      26th Oct 2004
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.


 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      26th Oct 2004
"A Traveler" <hitchhikersguideto-(E-Mail Removed)> schrieb:
> 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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


 
Reply With Quote
 
Imran Koradia
Guest
Posts: n/a
 
      26th Oct 2004
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 Traveler" <hitchhikersguideto-(E-Mail Removed)> wrote in message
news:u%(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
A Traveler
Guest
Posts: n/a
 
      26th Oct 2004
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.


"A Traveler" <hitchhikersguideto-(E-Mail Removed)> wrote in message
news:u%(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
Ken Halter
Guest
Posts: n/a
 
      26th Oct 2004
A Traveler wrote:
> 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

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
 
Reply With Quote
 
A Traveler
Guest
Posts: n/a
 
      26th Oct 2004
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.



"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:(E-Mail Removed)...
>A Traveler wrote:
>> 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
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Please keep all discussions in the groups..



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      26th Oct 2004
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/de...nethowto13.asp

http://msdn.microsoft.com/library/de...anagedapps.asp

Hope this helps
Jay

"A Traveler" <hitchhikersguideto-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>
>
> "A Traveler" <hitchhikersguideto-(E-Mail Removed)> wrote in message
> news:u%(E-Mail Removed)...
>>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.
>>

>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      26th Oct 2004
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...timization.pdf


Hope this helps
Jay


"A Traveler" <hitchhikersguideto-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>
>
> "Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
> news:(E-Mail Removed)...
>>A Traveler wrote:
>>> 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
>>
>> --
>> Ken Halter - MS-MVP-VB - http://www.vbsight.com
>> Please keep all discussions in the groups..

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AndAlso Al G Microsoft VB .NET 20 27th Aug 2007 10:09 PM
When to use AndAlso vs And ? =?Utf-8?B?UmljaA==?= Microsoft VB .NET 30 4th Jan 2007 06:42 AM
AndAlso & OrElse Raterus Microsoft VB .NET 12 5th Aug 2004 01:35 AM
AndAlso Matt Microsoft VB .NET 22 12th Jan 2004 10:07 PM
The Ballad of AndAlso and OrElse Jeremy Microsoft VB .NET 14 29th Aug 2003 12:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:37 AM.