473,665 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6478
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%******** ********@TK2MSF TNGP10.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%******** ********@TK2MSF TNGP10.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%******** ********@TK2MSF TNGP10.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%******** ********@TK2MSF TNGP10.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 ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
ResultsFromInte rnetSearch(Some SearchTerm) > 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 ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
ResultsFromInte rnetSearch(Some SearchTerm) > 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_Hotm ail.com> wrote in message
news:ed******** ******@TK2MSFTN GP15.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 ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
ResultsFromInte rnetSearch(Some SearchTerm) > 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_Hotm ail.com> wrote in message
news:ed******** ******@TK2MSFTN GP15.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 ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
ResultsFromInte rnetSearch(Some SearchTerm) > 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4626
by: Mike Hale | last post by:
Is it better to use AndAlso and OrElse by default rather than the regual And & Or? MikeH
22
3539
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
378
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 performing short-circuiting on an AndAlso? Purely for enlightenment. Thanks in advance. - Arthur Dent.
9
10374
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 And and OrElse instead of Or ?
30
4569
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
19238
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 Module Main Function reflect(x as boolean) as boolean
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.