Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Optimization of if

 
 
Ča\\/b
Guest
Posts: n/a
 
      02-02-2007
On Fri, 02 Feb 2007 11:14, Chris Dollin <(E-Mail Removed)> wrote:

no; the only error the code i posted seems to not include <time.h>

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


better u32? better i32?
why not?

>#dontdefine f
>#dontdefine u


is it not good define "f" and "u" because there is some relation in
language constant like to write "a=282u;" or "b=878.8f;"?
(what is wrong? "8u" is a word and not 2 words so #define should not
apply there)

>#dontdefine r
>#dontdefine w
>#dontdefine ll
>#dontdefine p


don't know why all others are not ok ( in the case these defines are
in all sources the same defines)
 
Reply With Quote
 
 
 
 
Dave Hansen
Guest
Posts: n/a
 
      02-02-2007
On Feb 1, 11:12 pm, Christopher Layne <(E-Mail Removed)> wrote:
> Richard wrote:
> >> 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.

>
> Pretty common style idiom for extremely simple maintenance logic. Get used to
> it. Obviously for a more complex case that is not filled with rudiment, you
> will see multiple lines. I just think you're ****ed off at the "style"
> itself.


I wouldn't inflict the above on my maintenance programmers,
particularly if X, Y and Z are less trivial than implied. But I'd
probably only add whitespace and some sensible indenting.

The coding standard of my present employer requires braces on all
conditional blocks and loops (thankfully not switch cases as well).
There are rational reasons for this requirement, so I grit my teeth
and follow it. I pick my fights more carefully. I'm even getting the
C++ guys to use PC-lint now. And they're discovering some interesting
things about their code...

Regards,
-=Dave

 
Reply With Quote
 
 
 
 
Jamie Boy
Guest
Posts: n/a
 
      02-07-2007
Rajen:


> if(X)
> {
> return retVal1;
> }
>
> if(Y)
> {
> return retVal2;
> }
>
> if(Z)
> {
> return retVal3;
> }

<snip>
> How do i Optimize this? Please give me some suggestions.



Put the one which is most likely to be true first. Failing that, I think your
code is at optimal efficiency.

--
~/JB\~
 
Reply With Quote
 
Christopher Layne
Guest
Posts: n/a
 
      02-12-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?


Alright, old article, but I just couldn't resist. I know some of you will get
a kick out of this:

http://thedailywtf.com/Articles/Turn...to_Eleven.aspx
 
Reply With Quote
 
Dietmar Schindler
Guest
Posts: n/a
 
      02-12-2007
Jamie Boy wrote:
>
> Rajen:
>
> > if(X)
> > {
> > return retVal1;
> > }
> >
> > if(Y)
> > {
> > return retVal2;
> > }
> >
> > if(Z)
> > {
> > return retVal3;
> > }

> <snip>
> > How do i Optimize this? Please give me some suggestions.

>
> Put the one which is most likely to be true first. ...


That can change the behaviour if more than one of X, Y, Z is true and
retVal1, retVal2, retVal3 are different.

--
DPS
 
Reply With Quote
 
Christopher Layne
Guest
Posts: n/a
 
      02-12-2007
Dietmar Schindler wrote:

> That can change the behaviour if more than one of X, Y, Z is true and
> retVal1, retVal2, retVal3 are different.
>


Excepting *exceptions*, the most common case should go first. The goal is to
get a hit asap.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-12-2007
Christopher Layne <(E-Mail Removed)> writes:
> Dietmar Schindler wrote:
>> That can change the behaviour if more than one of X, Y, Z is true and
>> retVal1, retVal2, retVal3 are different.

>
> Excepting *exceptions*, the most common case should go first. The goal is to
> get a hit asap.


That's *a* goal, but it doesn't make a whole lot of sense to optimize
the performance of your code at the cost of breaking its semantics
(unless you just want to get your wrong answers as quickly as
possible).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Christopher Layne
Guest
Posts: n/a
 
      02-13-2007
Keith Thompson wrote:

> Christopher Layne <(E-Mail Removed)> writes:
>> Dietmar Schindler wrote:
>>> That can change the behaviour if more than one of X, Y, Z is true and
>>> retVal1, retVal2, retVal3 are different.

>>
>> Excepting *exceptions*, the most common case should go first. The goal is
>> to get a hit asap.

>
> That's *a* goal, but it doesn't make a whole lot of sense to optimize
> the performance of your code at the cost of breaking its semantics
> (unless you just want to get your wrong answers as quickly as
> possible).
>


That's where the "exceptions" part came in.

e.g.

if (k > 40) return 2;
else if (k == 45) return 3; /* exception to above */

^^ NOT what I recommend.
 
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