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

## 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 on other sites
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 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 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 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 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;

##### 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 };

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

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

##### Share on other sites
This seems like what I've been looking for, thanks.

##### 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 arr3sum(arr1, arr1+size, arr2, arr3);

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by TheGilbOr 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.

1. 1
Rutin
65
2. 2
3. 3
4. 4
5. 5

• 17
• 10
• 29
• 20
• 9
• ### Forum Statistics

• Total Topics
633415
• Total Posts
3011768
• ### Who's Online (See full list)

There are no registered users currently online

×