code optimization

Discussion in 'C Programming' started by biswaranjan.rath, Feb 13, 2006.

  1. 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.
     
    biswaranjan.rath, Feb 13, 2006
    #1
    1. Advertising

  2. biswaranjan.rath

    tmp123 Guest

    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.
     
    tmp123, Feb 13, 2006
    #2
    1. Advertising

  3. biswaranjan.rath

    harry Guest

    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
     
    harry, Feb 13, 2006
    #3
  4. biswaranjan.rath

    Tim Prince Guest

    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.
     
    Tim Prince, Feb 13, 2006
    #4
  5. 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.
     
    August Karlstrom, Feb 13, 2006
    #5
  6. biswaranjan.rath

    Robin Haigh Guest

    "biswaranjan.rath" <> wrote in message
    news:...
    > 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
     
    Robin Haigh, Feb 13, 2006
    #6
  7. "biswaranjan.rath" <> wrote in message
    news:...
    > 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
     
    Rod Pemberton, Feb 13, 2006
    #7
  8. In article <>,
    "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.


    This shows clearly one of the dangers of optimization: Optimization can
    introduce bugs into your program.
     
    Christian Bau, Feb 13, 2006
    #8
  9. biswaranjan.rath

    Chris Torek Guest

    In article <>
    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))


    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 (40°39.22'N, 111°50.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.
     
    Chris Torek, Feb 14, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Wright

    Re: Code optimization...

    Pete Wright, Jul 5, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    396
    David Waz...
    Jul 6, 2003
  2. Russell Wallace
    Replies:
    7
    Views:
    415
    Russell Wallace
    Jun 11, 2004
  3. ashu

    optimization of vhdl code

    ashu, May 8, 2006, in forum: VHDL
    Replies:
    5
    Views:
    4,103
  4. joshc
    Replies:
    14
    Views:
    805
    Keith Thompson
    Jan 14, 2005
  5. Ravikiran

    Zero Optimization and Sign Optimization???

    Ravikiran, Nov 17, 2008, in forum: C Programming
    Replies:
    22
    Views:
    904
    Thad Smith
    Nov 24, 2008
Loading...

Share This Page