Optimization

Discussion in 'C Programming' started by InuY4sha, Apr 3, 2008.

  1. InuY4sha

    InuY4sha Guest

    How much can I save from this
    if (! (*ptr & MY_CONST) ){}
    and this
    if (*ptr != MY_CONST){}
    thanks, cheers
     
    InuY4sha, Apr 3, 2008
    #1
    1. Advertising

  2. InuY4sha

    Guest

    On Apr 3, 1:20 pm, InuY4sha <> wrote:
    > How much can I save from this
    > if (! (*ptr & MY_CONST) ){}
    > and this
    > if (*ptr != MY_CONST){}
    > thanks, cheers


    #define MYCONST 3
    *ptr = 1;

    Under these conditions the behavior will not be the same.
     
    , Apr 3, 2008
    #2
    1. Advertising

  3. InuY4sha

    Guest

    On Apr 3, 6:20 am, InuY4sha <> wrote:
    > How much can I save from this
    >        if (! (*ptr & MY_CONST) ){}
    > and this
    >        if (*ptr != MY_CONST){}
    > thanks, cheers


    Shouldn't that be an exclusive or operator in the first one to give
    you the same logical result.
     
    , Apr 3, 2008
    #3
  4. InuY4sha

    Ben Pfaff Guest

    InuY4sha <> writes:

    > How much can I save from this
    > if (! (*ptr & MY_CONST) ){}
    > and this
    > if (*ptr != MY_CONST){}


    You could omit the whole thing, since it has no side effects
    (unless ptr or *ptr is qualified with volatile). That would save
    you a line of code.
    --
    "All code should be deliberately written for the purposes of instruction.
    If your code isn't readable, it isn't finished yet."
    --Richard Heathfield
     
    Ben Pfaff, Apr 3, 2008
    #4
  5. InuY4sha

    John Bode Guest

    On Apr 3, 5:20 am, InuY4sha <> wrote:
    > How much can I save from this
    > if (! (*ptr & MY_CONST) ){}
    > and this
    > if (*ptr != MY_CONST){}
    > thanks, cheers


    You won't save anything, because the two forms will give you different
    answers for the same inputs. If *ptr and MY_CONST share *any* bits in
    common, the first form will evaluate to 0, which is not the same as
    the second form.

    It doesn't matter how fast your code is if it gives you the wrong
    answer. This kind of premature micro-optimization leads to code that
    is often buggy and difficult to maintain. You should only resort to
    this kind of trickery if a) you are failing to meet a hard performance
    requirement, b) profiling of the code reveals that this particular
    expression really is the bottleneck, c) the proposed change really
    buys you the performance you need, and d) the result of the optimized
    expression is the same as the unoptimized expression.

    For this situation, it's unlikely you'd see any appreciable difference
    in performance, but the only way to know for sure is to code up both
    versions, profile them, and compare the results. But again, the
    "optimized" version gives different results from the "unoptimized"
    version.
     
    John Bode, Apr 3, 2008
    #5
  6. InuY4sha wrote:
    > How much can I save from this
    > if (! (*ptr & MY_CONST) ){}
    > and this
    > if (*ptr != MY_CONST){}


    Your question, as stated, doesn't make any sense, since the two
    expressions are not equivalent.

    Anyway, replacing simple expression with another equivalent expression
    won't normally have any optimizing effect, because any half-decent
    compiler normally knows by itself the most optimal way (of many
    equivalent ways) to implement the operation.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Apr 3, 2008
    #6
    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. JE
    Replies:
    0
    Views:
    457
  2. Andrew Greensted

    Don't care and optimization

    Andrew Greensted, Jan 10, 2006, in forum: VHDL
    Replies:
    3
    Views:
    3,194
    Andrew Greensted
    Jan 11, 2006
  3. Replies:
    0
    Views:
    524
  4. Pete Wright

    Re: Code optimization...

    Pete Wright, Jul 5, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    387
    David Waz...
    Jul 6, 2003
  5. Ravikiran

    Zero Optimization and Sign Optimization???

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

Share This Page