469,270 Members | 1,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

And vs. AndAlso, performance??

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.
Nov 21 '05 #1
11 6243
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" <hi*********************@yahoo.com> wrote in message
news:u%****************@TK2MSFTNGP10.phx.gbl...
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.

Nov 21 '05 #2
"A Traveler" <hi*********************@yahoo.com> 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/>
Nov 21 '05 #3
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" <hi*********************@yahoo.com> wrote in message
news:u%****************@TK2MSFTNGP10.phx.gbl...
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.

Nov 21 '05 #4
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" <hi*********************@yahoo.com> wrote in message
news:u%****************@TK2MSFTNGP10.phx.gbl...
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.

Nov 21 '05 #5
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" <hi*********************@yahoo.com> wrote in message
news:u%****************@TK2MSFTNGP10.phx.gbl...
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.

Nov 21 '05 #6
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..
Nov 21 '05 #7
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..
Nov 21 '05 #8
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:ed**************@TK2MSFTNGP15.phx.gbl...
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..

Nov 21 '05 #9
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:ed**************@TK2MSFTNGP15.phx.gbl...
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..

Nov 21 '05 #10
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" <hi*********************@yahoo.com> wrote in message
news:uu*************@TK2MSFTNGP11.phx.gbl...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" <hi*********************@yahoo.com> wrote in message
news:u%****************@TK2MSFTNGP10.phx.gbl...
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.


Nov 21 '05 #11
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" <hi*********************@yahoo.com> wrote in message
news:O6**************@TK2MSFTNGP14.phx.gbl...
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:ed**************@TK2MSFTNGP15.phx.gbl...
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..


Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Mike Hale | last post: by
22 posts views Thread by Matt | last post: by
1 post views Thread by A Traveler | last post: by
30 posts views Thread by =?Utf-8?B?UmljaA==?= | last post: by
3 posts views Thread by Siegfried Heintze | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.