Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

Any faster way? (appending vectors of different types)

This topic is 2391 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

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!

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!