473,386 Members | 1,810 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,386 software developers and data experts.

Is Short-circuiting always a good idea?

I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for everything.

Thanks for any insights.
===
Phil
Nov 21 '05 #1
5 1310
On 2004-12-09, Phil Jones <ph************@hotmail.com> wrote:
I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for everything.


IMHO, it's a good habit for everything boolean. There's no downside. I
tend to think of AndAlso/OrElse as the boolean operators, while And/Or
are only for bitwise operations.

In fact, I can't remember the last time I used And/Or in a piece of
code.

Nov 21 '05 #2
Cool - that's good to know. Appreciate the advice David.
===
Phil
Nov 21 '05 #3

"Phil Jones" <ph************@hotmail.com> wrote
I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for everything.


Not only should you look into using short circuiting when you can, but
you must also place the operands in the proper order to gain the full
benefits from using short circuit evaluation.

Consider running this test every hour: (pseudo code)

If Now.Time = Noon AndAlso Now.Day = LeapDay Then ...

LeapDay (Feb 29) happens once every 4 years, but Noon happens once
each day. That means the second part (LeapDay) is tested at noon
every day, 365 days per year. for 4 years before the entire condition is
True.

But the reverse gains more benefit from the short circuit operator:

If Now.Day = LeapDay AndAlso Now.Time = Noon Then ...

In that siuation the second part is only tested 24 times, when LeapDay
has arrived.

That is an exaggerated situation, but it helps in pointing out that the
less frequent condition(s) need to be tested first, for optimal performance.

LFS
Nov 21 '05 #4
Larry,

With a leap month it is even more in some cultures

http://www.hermetic.ch/chcal/leap_months.htm

:-)

Cor
Nov 21 '05 #5
Phil,
There was a long discussion on short circuiting in this newsgroup a number
of months ago.

See the following thread.
http://groups-beta.google.com/group/...86fda008e54916

Hope this helps
Jay

"Phil Jones" <ph************@hotmail.com> wrote in message
news:eI**************@tk2msftngp13.phx.gbl...
I'm just (as a matter of course) using AndAlso and OrElse statements to
short circuit checking, even for nominal things like Boolean comparisons.
I'm wondering, is that a good thing to do (for performance reasons) or is
there some hidden down-side to it.

I can see why it's good for say not checking an object ref if it doesn't
exist - I'm just wondering if it's a good habit to get into for
everything.

Thanks for any insights.
===
Phil

Nov 21 '05 #6

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

Similar topics

18
by: a | last post by:
short s; long l; s= -2; l= -3; printf("% _ %_",s, l); What characters should be filled out in the formatted string for output? Thanx
8
by: Ken Dopierala Jr. | last post by:
Hi, I'm reading the header file of a PCX image and I need to convert 2 bytes to a short. How would I go about doing this? I know that they are bytes 8 & 9 in my byte array. I'm not sure how to...
7
by: LaMoRt | last post by:
Hi I want to ask how to convert from long to short since i have some parameter problem with it? The problem is like below : short *speech; // Holds length of any given sample *...
15
by: Steffen Loringer | last post by:
Hi, I'm using the following function to join 2 char (byte) into one short on a 32 bit X86 platform: unsigned short joinUnsigShort(unsigned char a,unsigned char b) { unsigned short val = 0;...
4
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
10
by: nyhetsgrupper | last post by:
The following code result in the following compilation error: Error 1 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) short a = 1; short...
17
by: Martin Wells | last post by:
When passing arguments to a VAL function, all integer types are promoted, and floats become doubles. Therefore, signed short will be promoted to signed int. Therefore I can't see any reason why...
17
by: spasmous | last post by:
I need a way to search through a block of memory for a char array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0;...
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...

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.