code optimization

B

biswaranjan.rath

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.
 
T

tmp123

biswaranjan.rath said:
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.
 
H

harry

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
 
T

Tim Prince

biswaranjan.rath said:
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.
 
A

August Karlstrom

biswaranjan.rath said:
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
 
R

Robin Haigh

biswaranjan.rath said:
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.
 
R

Rod Pemberton

biswaranjan.rath said:
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
 
C

Christian Bau

"biswaranjan.rath said:
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.
 
C

Chris Torek

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?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top