|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Should I resize std::lists and vectors before using? |
|
![]() freeworld Member since: 9/7/2007 From: friday harbor, WA, United States |
||||
|
|
||||
| I'm bored and curious how much this effects the way I'm using them. I've got a std::list that I populate with structs variables, each frame. Filling it with upwards of 10-20K structs. and at the end of the frame clear all these structs. and start adding new one at the beggining of the frame. couple questions. And would I knotice a difference in speed at all? 1# would it be wise to resize the list/vector as soon as I create it, to a big enough value to hold the minimum amout of data. somthing like (sizeof(struct) * 10K)? 2# Should I be resizeing the list to '0' after clearing it? |
||||
|
||||
![]() Twisol Member since: 9/11/2005 From: Los Angeles |
||||
|
|
||||
| I'd question why you need to fill it with 10-20k individual structures per frame, personally... 1. You can call .reserve() on it to reserve enough space for however many structs you think you'll need. If you push onto a vector that's hit its reserve, it allocates a lot more space, but the allocation might take a slice of time longer than you might want. Just reserve() the right amount when the vector's created and you won't need to worry about its re-allocation and copying. 2. No, because that would force the vector to resize itself again and again every frame. Maybe if memory was critical instead of speed, but only if it was some code that wasn't executed so often. Per-frame can be quite often. |
||||
|
||||
![]() freeworld Member since: 9/7/2007 From: friday harbor, WA, United States |
||||
|
|
||||
Quote: My 2D Engine/Renderer stores a list of or should I say several list of all the draw commands that have been used. Some of these are dynamic and therefore change every frame. Yes, I have had it at 20K stored commands one time to test the limits of how high I could go while keeping a normal frame rate. I'll try the reserve and see if that lowers my frame time at all. but like I said I was just bored starring at my code last nite and just thought of this, so thought I might as well try and ask. |
||||
|
||||
![]() Drew_Benton GDNet+ Member since: 7/29/2004 From: Katy, TX, United States |
||||
|
|
||||
Quote: Just to be clear, when you resize/reserve a container, you work with quantity of objects, not the total size of objects. Likewise when you call the default new operator to allocate an array of objects, you simply pass the quantity of items you want allocated, not the total size of bytes of memory to allocate (that's C's malloc!) If you did happen to pass something like sizeof(struct) * 10K), you would be allocating way too much internal memory, so pay careful attention to this! |
||||
|
||||
![]() SriLumpa Member since: 3/3/2009 |
||||
|
|
||||
| (I think he has 10k *of* the structs.) Maybe you can store only pointers to those structs in the vector, to reduce size. You'd still have to handle allocation somewhere, but the non-dynamic kind could be static. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|