Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Short circuiting..

Reply
Thread Tools

Short circuiting..

 
 
ram
Guest
Posts: n/a
 
      04-03-2004
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;
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-03-2004
On 3 Apr 2004 02:03:50 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-03-2004
On 3 Apr 2004 02:03:50 -0800, (E-Mail Removed) (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>>
 
Reply With Quote
 
ram
Guest
Posts: n/a
 
      04-09-2004
.....

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
unsigned short, short literals Ioannis Vranos C Programming 5 03-05-2008 01:25 AM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM
Wireless distance far too short M Skabialka Wireless Networking 3 01-14-2005 07:51 PM



Advertisments