Re: Code optimisation

Discussion in 'C++' started by Agent Mulder, Aug 27, 2003.

  1. Agent Mulder

    Agent Mulder Guest

    <Pete>
    I'm trying to optimise some code for speed.
    Essentially I have a big loop with a kernel of the form

    double *out, *in;
    int *test;

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

    On my machine I find that about the same performance is obtained with

    out = test*(4*in);

    Is this to be expected with all compilers and h/w and is there a faster way
    of doing this?
    </Pete>

    You can soup it up like this:

    out=test?4*in:.0;

    Better still it would be to store the
    contents of in, out and test in a
    struct. Them make a list<struct>,
    kick out all invalid elements and do
    the processing only on the valid
    elements. I can help you with it if
    you send some compilable code.

    -X
     
    Agent Mulder, Aug 27, 2003
    #1
    1. Advertising

  2. > double *out, *in;
    > int *test;
    >
    > if(test)
    > out = 4*in
    > else
    > out = 0.0;
    >
    > On my machine I find that about the same performance is obtained with
    >
    > out = test*(4*in);
    >
    > Is this to be expected with all compilers and h/w and is there a

    faster way
    > of doing this?
    > </Pete>
    >
    > You can soup it up like this:
    >
    > out=test?4*in:.0;


    With a decent optimizing compiler it is very unlikely that will make a
    performance difference.

    > Better still it would be to store the
    > contents of in, out and test in a
    > struct. Them make a list<struct>,
    > kick out all invalid elements and do
    > the processing only on the valid
    > elements.


    That strategy would only help if the test[] array remains constant so
    the list can be reused at a later time. If the list has to be generated
    every time the function is called, the process of generating the list
    itself would take more time than doing the actual computations. Anyway
    vector would probably be faster choice in this case.

    Optimization often means speeding up one operation at the expense of
    another. So the question remains what is the bigger picture? What is the
    size of the array? What is stored in the test array, lots of 0's and
    occasionally a '1'? Does 'test' change often...?
    --
    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. Rob Williscroft

    Re: Code optimisation

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

    Re: Code optimisation

    Rob Williscroft, Aug 27, 2003, in forum: C++
    Replies:
    1
    Views:
    361
    Peter van Merkerk
    Aug 27, 2003
  3. Peter van Merkerk

    Re: Code optimisation

    Peter van Merkerk, Aug 27, 2003, in forum: C++
    Replies:
    1
    Views:
    372
    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:
    296
    Farraige
    Nov 8, 2006
Loading...

Share This Page