473,382 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 6449
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mike Hale | last post by:
Is it better to use AndAlso and OrElse by default rather than the regual And & Or? MikeH
22
by: Matt | last post by:
Hi, I am new to vb.net. In using the If condition, can we use either of And or AndAlso. Whats the difference ? In VB I have always used And. For eg. if num > 10 AndAlso num < 20 Thanks. Matt
1
by: A Traveler | last post by:
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...
9
by: Lior | last post by:
Hello . I know that the AndAlso and OrElse statements are short-circuiting And and Or statements , respectively . Should I always use (I don't like the word "always" ...) AndAlso instead of...
30
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, If x = y And m = n The .... End If If x = y AndAlso m = n Then .... End If
3
by: Siegfried Heintze | last post by:
Are there operators in C# that are the counterparts of "OrElse" and "AndAlso" that I can use when translating the following program from VB.NET to C#? Thanks, Siegfried Namespace vbtest...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.