Re: Code optimisation

Discussion in 'C++' started by Rob Williscroft, Aug 27, 2003.

  1. Pete wrote in news:biim1a$7e3$:

    > Thanks for all the helpful responses.
    >
    > Fyi, test does indeed contain either 0 or 1.
    >
    > I did think about gathering the non zero terms but I don;t believe
    > this will be effecient because in reality there are several test's and
    > they change during the iteration.
    >
    > I like the idea of putting
    >
    > out += test ? in : 0;
    >
    > but I suspect (as one response noted) that most compilers will do
    > something like this anyway.
    >
    > As a matter of interest, are compilers smart enough not to bother
    > adding zero


    Yes.

    Though your original:

    if(test)
    out = 4*in
    else
    out = 0.0;

    Is actually no different from:

    out = test ? (4 * in) : 0.0;

    > - or is faster to add than to test?


    The test "... test ? ... " is in there anyway.

    Testing isn't the problem, its the jumping, you get a jump with
    'if else' statments and with the ?: operator. The problem is
    the CPU tries to predict which branch to take, this eats
    CPU resorces and potentialy slows it down. But whats faster
    is CPU/algorithm dependant:

    On current (i.e. expect it to change) desktop CPU's.

    integer math is probably faster than
    if else branches is probably faster than
    floating point math.

    But if you run your test on a Cray you'll get different results
    (Cray's are really good at floating point and indifferent at
    every thing else).

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Aug 27, 2003
    #1
    1. Advertising

  2. > On current (i.e. expect it to change) desktop CPU's.
    >
    > integer math is probably faster than
    > if else branches is probably faster than
    > floating point math.


    If we are talking about modern x86 architecture processors (Pentium, Athlon)
    the FP multiply is pretty fast as well (about as fast as integer multiply).
    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Aug 27, 2003
    #2
    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. Agent Mulder

    Re: Code optimisation

    Agent Mulder, Aug 27, 2003, in forum: C++
    Replies:
    1
    Views:
    317
    Peter van Merkerk
    Aug 27, 2003
  2. Rob Williscroft

    Re: Code optimisation

    Rob Williscroft, Aug 27, 2003, in forum: C++
    Replies:
    2
    Views:
    375
    Peter van Merkerk
    Aug 28, 2003
  3. Peter van Merkerk

    Re: Code optimisation

    Peter van Merkerk, Aug 27, 2003, in forum: C++
    Replies:
    1
    Views:
    371
    Alan Sung
    Aug 27, 2003
  4. mjm

    Re: Code optimisation

    mjm, Aug 29, 2003, in forum: C++
    Replies:
    2
    Views:
    356
    Peter van Merkerk
    Aug 29, 2003
  5. Farraige
    Replies:
    4
    Views:
    295
    Farraige
    Nov 8, 2006
Loading...

Share This Page