Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Code optimisation

Reply
Thread Tools

Re: Code optimisation

 
 
Agent Mulder
Guest
Posts: n/a
 
      08-27-2003
<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[i])
out[i] = 4*in[i]
else
out[i] = 0.0;

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

out[i] = test[i]*(4*in[i]);

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[i]=test[i]?4*in[i]:.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


 
Reply With Quote
 
 
 
 
Peter van Merkerk
Guest
Posts: n/a
 
      08-27-2003
> double *out, *in;
> int *test;
>
> if(test[i])
> out[i] = 4*in[i]
> else
> out[i] = 0.0;
>
> On my machine I find that about the same performance is obtained with
>
> out[i] = test[i]*(4*in[i]);
>
> 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[i]=test[i]?4*in[i]:.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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
please help with optimisation of this code - update of given table according to another table Farraige Python 4 11-08-2006 07:58 PM
Re: Code optimisation mjm C++ 2 08-29-2003 11:19 AM
Re: Code optimisation Rob Williscroft C++ 2 08-28-2003 07:39 AM
Re: Code optimisation Peter van Merkerk C++ 1 08-27-2003 09:26 PM
Re: Code optimisation Rob Williscroft C++ 1 08-27-2003 07:49 PM



Advertisments