adding arrays

Started by
10 comments, last by dmatter 15 years, 10 months ago
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]
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.
-------------Please rate this post if it was useful.
Do a lazy addition: only compute the values that you need. If you need them all, then you have to compute them all.
I think the question really is, why are you needing to do this operating to begin with? What are you trying to achieve?
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 membersfloat *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;
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 }
This seems like what I've been looking for, thanks.
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 arr3sum(arr1, arr1+size, arr2, arr3);
Or if you were to change your dynamic arrays into C-style arrays of fixed size:

// define the array extentsstatic const unsigned int ARRAY_SIZE_MAX = 2048;// allocate storage for the arraysstatic int s_Array1[ARRAY_SIZE_MAX];static int s_Array2[ARRAY_SIZE_MAX];// copy the contents of s_Array1 into s_Array2memcpy( 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.
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.

This topic is closed to new replies.

Advertisement