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

Short circuiting..

ram
hi all,
apart from readability, and not wanting the otherwise different
statements to be assumed as one single loong statement [ the comma
operator ]
what are the pros/cons of both snippets?

i tried this out on a small piece of code and the latter part using
short circuiting takes about half the time as the if else version;
gcc 3.2 , amdk6-2 running vectorlinux;

cheers
ram

herz a snippet of code..
================== 1
int flag = f ;

if( f)
{ if1() ;
if2() ;
if3() ;
}
else
{ e1() ;
e2() ;
}
==================== 2 =
int flag = f ;
(
(
( f ) && ( if1() ,
if2() ,
if3()
)
) ||
( e1() , e2()
)
)

)

=================== end of snippets;
Nov 14 '05 #1
3 2107
On 3 Apr 2004 02:03:50 -0800, ci******@yahoo.com (ram) wrote:
hi all,
apart from readability, and not wanting the otherwise different
statements to be assumed as one single loong statement [ the comma
operator ]
what are the pros/cons of both snippets?

i tried this out on a small piece of code and the latter part using
short circuiting takes about half the time as the if else version;
gcc 3.2 , amdk6-2 running vectorlinux;

cheers
ram

herz a snippet of code..
================== 1
int flag = f ;

if( f)
{ if1() ;
if2() ;
if3() ;
}
else
{ e1() ;
e2() ;
}
==================== 2 =
int flag = f ;
(
(
( f ) && ( if1() ,
if2() ,
if3()
)
) ||
( e1() , e2()
)
)

)


I'm really suspicious of your timing results, because even using my old
non-optimizing C compiler from 1979, I would not have expected measurably
different results from those two constructs.

I'm going to go out on a limb here and show C++ code, because I happen to
have a convenient C++ timing utility. I apologize in advance if this
offends anyone; I hope folks will understand the motivation:. I'm going to
make the bold assumption that code generation facilities for if/else vs.
short-circuit conditional evaluation are going to be pretty much the same
across C and C++.

So here's my test program:

#include <iostream>
#include "ESTLUtil.h"
using namespace std;
using namespace ESTLUtils;

void if1(){}
void if2(){}
int if3(){ return 1;}
void e1(){}
int e2(){ return 1;}

#define ITERATIONS 100000000

int main()
{
int f = 1;
int i;

{
Timer t;
for (i = 0; i < ITERATIONS; ++i)
{
if( f)
{ if1() ;
if2() ;
if3() ;
}
else
{ e1() ;
e2() ;
}
}
cout << "Using if/else (f = " << f << "): " << t << endl;
}

{
Timer t;
for (i = 0; i < ITERATIONS; ++i)
{
int flag = f ;
(
(
( f ) && ( if1() ,
if2() ,
if3()
)
) ||
( e1() , e2()
)
);
}
cout << "Using &&/||e (f = " << f << "): " << t << endl;
}
return 0;
}
Note I had to "pick" a logical return value for if3 and e2 (in the second
section) in order for the conditionals to compile. I just used 1.

I ran it for f equal to 0 and again for f equal to 1. results:

d:\src\learn>snip
Using if/else (f = 0): 1.472
Using &&/||e (f = 0): 1.592

d:\src\learn>snip
Using if/else (f = 1): 2.153
Using &&/||e (f = 1): 2.133

I got slightly different results each time, with the if/else /usually/
being a tiny bit faster (although they often came in equal). Since this
timing utility records real time and not CPU time, Windows task activity
can account for the variations. I don't a Unix box to run a "time" test
on.

In any case, hardly a case of the timing discrepancy you described.

I'd stick to the if/else.

BTW, if anyone cares to try this with the timing utility I'm using, the
header file (ESTLUtil.h) is part of each of the "InitUtil" distributions
available on my web site.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
On 3 Apr 2004 02:03:50 -0800, ci******@yahoo.com (ram) wrote:
hi all,
apart from readability, and not wanting the otherwise different
statements to be assumed as one single loong statement [ the comma
operator ]
what are the pros/cons of both snippets?

i tried this out on a small piece of code and the latter part using
short circuiting takes about half the time as the if else version;
gcc 3.2 , amdk6-2 running vectorlinux;

cheers
ram
First off, they don't behave the same. If f is non-zero:

Code 1 will execute if1, if2, and if3 and never execute e1 or e2.

Code 2 will execute if1, if2, and if3. If if3 evaluates to
anything that compares equal to 0, it will also execute e1 and e2.

Secondly, is code 2 valid if if3 evaluates to void?

Thirdly, code 2 has more ')' than '('

herz a snippet of code..
================== 1
int flag = f ;

if( f)
{ if1() ;
if2() ;
if3() ;
}
else
{ e1() ;
e2() ;
}
==================== 2 =
int flag = f ;
(
(
( f ) && ( if1() ,
if2() ,
if3()
)
) ||
( e1() , e2()
)
)

)

=================== end of snippets;


<<Remove the del for email>>
Nov 14 '05 #3
ram
.....

hi,

Secondly, is code 2 valid if if3 evaluates to void?

no its not!! i had to stuff a 1 at the end;
Thirdly, code 2 has more ')' than '('

The snippet was typed seperately..

any way it was my mistake.. i did not test it properly.
sorry :( ;

for a few runs of a trivial program that just prints where it is
printing from.. the output of /usr/bin/time indicated that the if-else code
took 0m0.020s user time while the other one took 0m0.010s ;
for five or six runs it was same..
but when i add it up over 100,000 runs.. it looks like they are pretty
much the same;

but funny thing is time reports 0m0.000s also for some runs:) ;
what is a better way of testing?
cheers
ram
Nov 14 '05 #4

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

Similar topics

16
by: Dave Opstad | last post by:
In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that...
10
by: Niels Dekker (no reply address) | last post by:
Is it possible for a standard compliant C++ compiler to have ( sizeof(short) < sizeof(int) ) and ( sizeof(short) == sizeof((short)0 + (short)0) ) ? Regards, Niels Dekker...
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
3
by: ram | last post by:
hi all, apart from readability, and not wanting the otherwise different statements to be assumed as one single loong statement what are the pros/cons of both snippets? i tried this out on a...
5
by: Michael Jørgensen | last post by:
Hi there, Is there any difference between bool success = SomeFunctionReturningFalse(); success &= SomeOtherFunction(); and bool success = SomeFunctionReturningFalse();
2
by: Pho Bo Vien | last post by:
I have several link buttons on a page and many validation controls. I want the validation controls to block the user from submitting the form unless their conditions are satisfied -- however, the...
6
by: Raymond Lewallen | last post by:
I understand the differences between And and AndAlso and the differences between Or and OrElse. Under what circumstance would you NOT use short-circuiting when evaluating expressions? It seems to...
5
by: Phil Jones | last post by:
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.