Jump to content
  • Advertisement
Sign in to follow this  
eppo

adding arrays

This topic is 3816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way (in C++) to quickly add the contents of one array to another same-sized array without having to iterate through the entire array? (I have two super-large arrays that need to be added on a per-frame basis and I'm trying to speed this up.) [Edited by - eppo on June 9, 2008 7:49:15 AM]

Share this post


Link to post
Share on other sites
Advertisement
It's obviously impossible to use the contents in a container without referencing said contents, which requires that you iterate through the container. To make things go faster, you can parallelize your algorithm or, I guess, use SSE instructions.

Share this post


Link to post
Share on other sites
Do a lazy addition: only compute the values that you need. If you need them all, then you have to compute them all.

Share this post


Link to post
Share on other sites
I think the question really is, why are you needing to do this operating to begin with? What are you trying to achieve?

Share this post


Link to post
Share on other sites
I have a list of structures that define object properties in 3d space.
And I suppose this is the fastest way:


//objectProperties has eight float-data members
float *pos = (float*)objectProperties, *posD = (float*)objectPropertiesD;
for(int i = 0; i < objectCount * 8; i++){ *pos += *posD; pos++, posD++; }



or maybe this:

for(int i = 0; i < objectCount * 8; i++) pos += posD;

Share this post


Link to post
Share on other sites
Assuming by 'add' you mean 'sum' then you can use the std::transform function with the std::plus functor:


// two arrays
const int size = 4;
int arr1[size] = { 1, 2, 4, 9 };
int arr2[size] = { 3, 5, 6, 8 };

// add arr1 to arr2
std::transform(arr1, arr1 + size, arr2, arr2, std::plus<int>());

// now: arr2 == { 4, 7, 10, 17 }

Share this post


Link to post
Share on other sites
If you plan to use it often then I'd suggest you wrap it into a utility function:

template < class InputIterator1, class InputIterator2, class OutputIterator >
void sum(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result)
{
typedef ::plus<std::iterator_traits<InputIterator1>::value_type> Add;
std::transform(first1, last1, first2, result, Add());
}

// add arr1 and arr2 and place the result in arr3
sum(arr1, arr1+size, arr2, arr3);

Share this post


Link to post
Share on other sites
Or if you were to change your dynamic arrays into C-style arrays of fixed size:

// define the array extents
static const unsigned int ARRAY_SIZE_MAX = 2048;

// allocate storage for the arrays
static int s_Array1[ARRAY_SIZE_MAX];
static int s_Array2[ARRAY_SIZE_MAX];

// copy the contents of s_Array1 into s_Array2
memcpy( s_Array2, s_Array1, sizeof(s_Array1) );

Even C++ template magic won't be faster than a decent memcpy implementation. Still, remember that the fastest code is the code that isn't written.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheGilb
Or if you were to change your dynamic arrays into C-style arrays of fixed size:

*** Source Snippet Removed ***
Even C++ template magic won't be faster than a decent memcpy implementation. Still, remember that the fastest code is the code that isn't written.


I believe he wants to place the sum in the destination array, not the copy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!