Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Optimization of if

Reply
Thread Tools

Optimization of if

 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-02-2007
Christopher Layne said:

> Chris Dollin wrote:
>
>> Myself I prefer the form:
>>
>> return
>> X ? retVal1
>> : Y ? retVal2
>> : Z ? retVal3
>> : whateverYouWantForNoneOfXYZ
>> ;
>>
>> laid out in whatever way fits your coding style.

>
> Do you like pain?


Why do you ask? It looks self-documenting and pain-free to me.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      02-02-2007
Richard wrote:

> CBFalconer <(E-Mail Removed)> writes:


>> int retval;
>>
>> if (X) retval = retval1;
>> else if (Y) retval = retval2;
>> else if (Z) retval = retval3;
>> else retval = 0;
>> return retval;

>
> In commercial SW, multiple statements/expressions on a line are neither
> clearer nor more "compact".


They look clear and more compact to /me/.

> The layout above is horrible


It's spelt b e a u t i f u l.

> and totally debugger unfriendly.


Never been a problem for me.

> The excessive whitespace breaks the flow of a code
> read.


Maybe.

> the lack of indentation doesnt hilite the conditional
> assignments.


Who cares?

> 0 out of 10.


It's the pointless variable I hate and despise.

--
Chris "electric hedgehog" Dollin
Meaning precedes definition.

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      02-02-2007
Christopher Layne wrote:

> Chris Dollin wrote:
>
>> Myself I prefer the form:
>>
>> return
>> X ? retVal1
>> : Y ? retVal2
>> : Z ? retVal3
>> : whateverYouWantForNoneOfXYZ
>> ;
>>
>> laid out in whatever way fits your coding style.

>
> Do you like pain?


MYOB.

Did you have a constructive comment to offer?

--
Chris "electric hedgehog" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

 
Reply With Quote
 
Ča\\/b
Guest
Posts: n/a
 
      02-02-2007
On Thu, 01 Feb 2007 17:37:28 -0500, CBFalconer <(E-Mail Removed)>
wrote:

>Richard Tobin wrote:
>> Rajen <(E-Mail Removed)> wrote:
>>
>>> I have a code like this.
>>> if(X)
>>> {
>>> return retVal1;
>>> }
>>>
>>> if(Y)
>>> {
>>> return retVal2;
>>> }
>>>
>>> if(Z)
>>> {
>>> return retVal3;
>>> }
>>>
>>>
>>> The above if can be written like this
>>> int retVal = 0;
>>> if(X)
>>> {
>>> retVal=retVal1;
>>> }
>>>
>>> if( retVal == 0)
>>> {
>>> if( Y)
>>> retVal=retVal2;
>>> }
>>>
>>> if( retVal == 0 )
>>> {
>>> if(Z)
>>> retVal=retVal3;
>>> }
>>>
>>> return retVal;
>>> which has only one exit point. But it has more If's

>>
>> Your first form is much clearer, so use that. If your employer
>> insists on less clear code to satisfy some rules, find another
>> employer.

>
>No, just write clearer and more compact code:
>
> int retval;
>
> if (X) retval = retval1;
> else if (Y) retval = retval2;
> else if (Z) retval = retval3;
> else retval = 0;
> return retval;


what says compiler about this:
return X?retval1:Y?retval2:Z?retvalz:0:0:0;
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      02-02-2007
Ča\/b wrote:

> what says compiler about this:
> return X?retval1:Y?retval2:Z?retvalz:0:0:0;


It says it's a syntax error, and I agree with it.

--
Chris "electric hedgehog" Dollin
"Who are you? What do you want?" /Babylon 5/

 
Reply With Quote
 
Ča\\/b
Guest
Posts: n/a
 
      02-02-2007
On Fri, 02 Feb 2007 09:44:01 +0000, Chris Dollin <(E-Mail Removed)>
wrote:
>?\/b wrote:
>
>> what says compiler about this:
>> return X?retval1:Y?retval2:Z?retvalz:0:0:0;

>
>It says it's a syntax error, and I agree with it.



and what says about:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define uns unsigned

uns fn(uns x,uns y,uns z){return x?1:y?2:z?3:0;}

int main(void)
{uns x, y, z;

srand(time(0));
x=rand(); y=rand(); z=rand();
printf("fn(%u, %u, %u)=%u\n", x, y, z, fn(x, y, z));
printf("fn(%u, %u, %u)=%u\n", 1, 0, 0, fn(1, 0, 0));
printf("fn(%u, %u, %u)=%u\n", 0, 1, 0, fn(0, 1, 0));
printf("fn(%u, %u, %u)=%u\n", 0, 0, 1, fn(0, 0, 1));
printf("fn(%u, %u, %u)=%u\n", 0, 0, 0, fn(0, 0, 0));
return 0;
}

>--
>Chris "electric hedgehog" Dollin
>"Who are you? What do you want?" /Babylon 5/


"return x?1:y?2:z?3:0;"
seems more clear than
"
if(x) r=1;
else if(y) r=2;
else if(z) r=3;
else r=0;
return r;
"

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      02-02-2007
Ča\/b wrote:

> On Fri, 02 Feb 2007 09:44:01 +0000, Chris Dollin <(E-Mail Removed)>
> wrote:
>>?\/b wrote:
>>
>>> what says compiler about this:
>>> return X?retval1:Y?retval2:Z?retvalz:0:0:0;

>>
>>It says it's a syntax error, and I agree with it.

>
> and what says about:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
> #define uns unsigned
>
> uns fn(uns x,uns y,uns z){return x?1:y?2:z?3:0;}


It says "stupid #define `uns`.

> "return x?1:y?2:z?3:0;"
> seems more clear than
> "
> if(x) r=1;
> else if(y) r=2;
> else if(z) r=3;
> else r=0;
> return r;
> "


Well, I think so too. Of course the code /I/ showed was (a) right
the first time, (b) preserved the OPs names for values, (c) exploited
whitespace, (d) carried an explicit "your layout may vary" message,
and (e) appeared yesterday.

Below you will find some whitespace you may care to use in your code,
and some dontdefines you may find useful.

.
.
.
.
.
.
.
.

#dontdefine uns
#dontdefine f
#dontdefine u
#dontdefine r
#dontdefine w
#dontdefine ll
#dontdefine p

--
Chris "electric hedgehog" Dollin
Meaning precedes definition.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      02-02-2007
Chris Dollin <(E-Mail Removed)> wrote:

> Ča\/b wrote:
>
> > and what says about:


> > #define uns unsigned


> It says "stupid #define `uns`.


Interesting. Mine says: "Don't reply to trolls, please."

Richard
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      02-02-2007
Keith Thompson <(E-Mail Removed)> wrote:

> http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Bos) writes:
> > "Rajen" <(E-Mail Removed)> wrote:
> > > if(X)
> > > {
> > > return retVal1;
> > > }
> > >
> > > if(Y)
> > > {
> > > return retVal2;
> > > }
> > >
> > > if(Z)
> > > {
> > > return retVal3;
> > > }

> [...]
> > As others have noted, you're missing a default value.
> >
> > Try this:
> >
> > retval=default_value;
> > if (Z) retval=retval3;
> > if (Y) retval=retval2;
> > if (X) retval=retval1;
> > return retval;

>
> That's likely to be (slightly) *less* efficient, since it can assign
> to retval multiple times. It can also behave differently if X, Y, and
> Z are expressions that depend on each other's evaluation; for example,
> evaluating Y might not work if X hasn't already been evaluated.


True. But at least it does have the sacred One Return Statement, and
doesn't have the Scary else Keyword.

> > Then immediately return to the first, maintainable version, but lay it
> > out a bit more legibly:
> >
> > if(X) return retVal1;
> > if(Y) return retVal2;
> > if(Z) return retVal3;
> >
> > or if you like braces:
> >
> > if(X) { return retVal1; }
> > if(Y) { return retVal2; }
> > if(Z) { return retVal3; }

>
> I'd probably lay it out that way if the conditions and result
> expressions really were that terse, except that I always put a blank
> after an "if" (it's not a function call, so it shouldn't look like
> one):


So would I, and I did in the code above which I retyped rather than
copied-and-pasted. For the same reason as you do, BTW.

> But in real life, everything is likely to be longer, perhaps too long
> to fit on a single line. And if the conditions really were "X", "Y",
> and "Z", I'd seriously consider using names that make some actual
> sense.


They'll often be longer, but not always. I've not infrequently had code
where the conditions were something like

if (x<0) x=0;
if (y<0) y=0;
if (x>size) x=size;
if (y>size) y=size;

In context, those x and y did make sense: they were cartesian
coordinates. Possibly they were start_x and start_y, plus end_x and
end_y.

Richard
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-02-2007
Richard wrote:
> CBFalconer <(E-Mail Removed)> writes:
>

.... snip ...
>>
>> No, just write clearer and more compact code:
>>
>> int retval;
>>
>> if (X) retval = retval1;
>> else if (Y) retval = retval2;
>> else if (Z) retval = retval3;
>> else retval = 0;
>> return retval;

>
> In commercial SW, multiple statements/expressions on a line are
> neither clearer nor more "compact". The layout above is horrible
> and totally debugger unfriendly. The excessive whitespace breaks
> the flow of a code read. the lack of indentation doesnt hilite
> the conditional assignments. 0 out of 10.


If you have to use a debugger on the above snippet, there is
something seriously wrong with your reading comprehension. You are
obviously a slave to poorly thought out so called 'coding
standards'.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


 
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
Zero Optimization and Sign Optimization??? Ravikiran C Programming 22 11-24-2008 03:19 AM
Don't care and optimization Andrew Greensted VHDL 3 01-11-2006 11:47 AM
BIOS Optimization Guide Rev. 8.21 Silverstrand Front Page News 0 08-24-2005 01:46 PM
Optimization problem, for a sports tournament JE Perl 0 08-04-2004 06:52 PM
will Synopsys Design Compiler automatically collect common sub expression to do intelligent optimization? walala VHDL 6 09-25-2003 11:43 AM



Advertisments