Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > code optimization

Reply
Thread Tools

code optimization

 
 
biswaranjan.rath
Guest
Posts: n/a
 
      02-13-2006
Is it better to have multiple if statements rather having multiple
conditions in single if statement ?

which one is better:
if (cond1 || cond2 && (cond3 || cond4) || cond5)

if(cond1 || cond5)
if(cond2 && (cond3 || cond4))

this is an example, there might be more conditions.

 
Reply With Quote
 
 
 
 
tmp123
Guest
Posts: n/a
 
      02-13-2006

biswaranjan.rath wrote:
> Is it better to have multiple if statements rather having multiple
> conditions in single if statement ?
>
> which one is better:
> if (cond1 || cond2 && (cond3 || cond4) || cond5)
>
> if(cond1 || cond5)
> if(cond2 && (cond3 || cond4))
>
> this is an example, there might be more conditions.


Hi,

Taken into account you have no "else" parts:

>From a point of view of performance and CPU cost, specially if your

compiler is able to optimize a few, it is the same.

>From a point of view of readable code, the first one is typically

(there are exceptions and opinions) more readable if you:
a) Place redundant parenthesis
b) mades good indentation
c) Adds comments.
d) Knowns enough first order logic to do it

Kind regards.

 
Reply With Quote
 
 
 
 
harry
Guest
Posts: n/a
 
      02-13-2006
I think it hardly matters either ways and it is more a matter of
choice. Testing multiple conditions (with proper paranthesis) sometimes
might save you an extra "if" check if it is done using multiple if's.
Also, most of the times having multiple if's will look bad (it kinda
breaks the check) compared to a single check for a set of boolean
conditions.

Harish

 
Reply With Quote
 
Tim Prince
Guest
Posts: n/a
 
      02-13-2006
biswaranjan.rath wrote:
> Is it better to have multiple if statements rather having multiple
> conditions in single if statement ?
>
> which one is better:
> if (cond1 || cond2 && (cond3 || cond4) || cond5)
>
> if(cond1 || cond5)
> if(cond2 && (cond3 || cond4))
>

question not answerable in context of standard C
In rare situations, certain compilers may be able to optimize the first
version better. If there are loop invariant groups in your conditional,
it might be better to write them explicitly, rather than depending so
much on the compiler and the ability of a human to parse your code.
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      02-13-2006
biswaranjan.rath wrote:
> Is it better to have multiple if statements rather having multiple
> conditions in single if statement ?
>
> which one is better:
> if (cond1 || cond2 && (cond3 || cond4) || cond5)
>
> if(cond1 || cond5)
> if(cond2 && (cond3 || cond4))
>
> this is an example, there might be more conditions.
>


Try both versions and measure. (The former is of course more readable.)


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
Robin Haigh
Guest
Posts: n/a
 
      02-13-2006

"biswaranjan.rath" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Is it better to have multiple if statements rather having multiple
> conditions in single if statement ?
>
> which one is better:
> if (cond1 || cond2 && (cond3 || cond4) || cond5)
>
> if(cond1 || cond5)
> if(cond2 && (cond3 || cond4))
>
> this is an example, there might be more conditions.



They aren't equivalent, so whichever is right is better.


And-ed conditions at the top level can be replaced by nested ifs, but with a
catch:

if ( cond1 && cond2 )
blah1;
else
blah2;

becomes

if ( cond1 )
if ( cond2 )
blah1;
else
blah2;
else
blah2; /* again */

Not pretty.

Or-ed conditions at top level (as in your example) are equivalent to
if-elses:

if ( cond1 || cond2 )
blah1;
else
blah2;

becomes

if ( cond1 )
blah1;
else if ( cond2 )
blah1; /* again */
else
blah2;

which isn't very pretty either.

On the occasions where you've really got a choice, it shouldn't make a
difference, and any difference it might make isn't going to be predictable.
If it really really matters, all you can do is look at the generated code on
your platform. Even then, you can't necessarily assume that it will be
consistent and infer any principles. For instance, if there are no side
effects or traps to worry about, it might apply the as-if rule and rearrange
the whole thing so as to test what's already to hand before testing what has
to be fetched or calculated.

--
RSH





 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      02-13-2006

"biswaranjan.rath" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Is it better to have multiple if statements rather having multiple
> conditions in single if statement ?
>
> which one is better:
> if (cond1 || cond2 && (cond3 || cond4) || cond5)
>
> if(cond1 || cond5)
> if(cond2 && (cond3 || cond4))
>
> this is an example, there might be more conditions.
>


The question is better how?
readability? speed? etc.??

If speed, the answer depends on the compiler's ability to optimize the code.
I've used some compilers that generate better code with many conditions in
the if(). On the other hand, I've used other compilers that generate better
code when less common conditions were in the nested if()'s. You'll need to
test it.


Rod Pemberton


 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      02-13-2006
In article <(E-Mail Removed). com>,
"biswaranjan.rath" <(E-Mail Removed)> wrote:

> Is it better to have multiple if statements rather having multiple
> conditions in single if statement ?
>
> which one is better:
> if (cond1 || cond2 && (cond3 || cond4) || cond5)
>
> if(cond1 || cond5)
> if(cond2 && (cond3 || cond4))
>
> this is an example, there might be more conditions.


This shows clearly one of the dangers of optimization: Optimization can
introduce bugs into your program.
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      02-14-2006
In article <(E-Mail Removed). com>
biswaranjan.rath <(E-Mail Removed)> wrote:
>Is it better to have multiple if statements rather having multiple
>conditions in single if statement ?
>
>which one is better:
>if (cond1 || cond2 && (cond3 || cond4) || cond5)
>
>if(cond1 || cond5)
> if(cond2 && (cond3 || cond4))


As Robin Haigh and Christian Bau noted, these are not the same
tests (they mean different things).

That aside -- or if you fix the tests -- consider an otherwise
equivalent question: "which flavor is better, chocolate or strawberry?"
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
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
optimization of vhdl code ashu VHDL 5 05-09-2006 07:12 PM
Compiler code optimization: see code below joshc C Programming 14 01-14-2005 01:02 AM
Optimization - any idea of actual machine code? Russell Wallace Java 7 06-11-2004 08:53 AM
Re: Code optimization... Pete Wright ASP .Net 2 07-06-2003 12:24 AM



Advertisments