Any faster way? (appending vectors of different types)

Started by
3 comments, last by floatingwoods 12 years, 5 months ago
Hi,

I have a function that takes quite long to execute, and that mainly appends data from various vectors to another vector of a different tye. If I can accelerate the appending part, then my whole function will get significantly faster.
How I do it now is:

std::vector<float> dataToAppend; // lets imagine that this one is filled with plenty of floats
std::vector<char> appendedData;

for (unsigned int i=0;i<dataToAppend.size();i++)
{
appendedData.push_back(((char*)&dataToAppend)[0]);
appendedData.push_back(((char*)&dataToAppend)[1]);
appendedData.push_back(((char*)&dataToAppend)[2]);
appendedData.push_back(((char*)&dataToAppend)[3]);
}

It doesn't really matter how the data is appended (e.g. little or big endian), since I will unpack it in a similar way later.

Thanks for any insight!
Advertisement
Off the top of my head:

char * ptr = reinterpret_cast<char *>(&dataToAppend[0]);
appendedData.insert(appendedData.end(), ptr, ptr + dataToAppend.size() * sizeof(float));

If appendedData is initially empty, you can also use assign() instead of insert().
Since you are using vectors they are allocated next to each other, this means that when you add new items it will declare a new block of memory and copy all of it in there plus the new one and free the old memory block. ( Lists are connected through memory scattered around, not next to each other so this will not need this block allocation and deallocation ). Because of this you might want to use the Reserve() method for your vector if you know in advance what the size is. This will make it allocate its memory in advance, this way when appending data it does not have to reallocate memory. You might also want to use this define:
#define _SECURE_SCL 0

This will remove the bounding checks when pushing data into your vector and/or list. This does mean, however, that you will have to make sure you don't go outside of the bounds in your own code, you are no longer protected.
If I've helped you in any way please push the reputation button, thanks!

Abstraction is my choice of words.
Portfolio: http://www.0x3a.com/
Blog: http://blog.0x3a.com/
Just what I was looking for! Thanks a lot SiCrane!


Off the top of my head:

char * ptr = reinterpret_cast<char *>(&dataToAppend[0]);
appendedData.insert(appendedData.end(), ptr, ptr + dataToAppend.size() * sizeof(float));

If appendedData is initially empty, you can also use assign() instead of insert().
0x3a, thanks too! I'll do that too!!


Since you are using vectors they are allocated next to each other, this means that when you add new items it will declare a new block of memory and copy all of it in there plus the new one and free the old memory block. ( Lists are connected through memory scattered around, not next to each other so this will not need this block allocation and deallocation ). Because of this you might want to use the Reserve() method for your vector if you know in advance what the size is. This will make it allocate its memory in advance, this way when appending data it does not have to reallocate memory. You might also want to use this define:
#define _SECURE_SCL 0

This will remove the bounding checks when pushing data into your vector and/or list. This does mean, however, that you will have to make sure you don't go outside of the bounds in your own code, you are no longer protected.

This topic is closed to new replies.

Advertisement