Home » Community » Forums » For Beginners » Should I resize std::lists and vectors before using?
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

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?
Post New Topic  Post Reply 
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?

 User Rating: 1008   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1245   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Quote:
Original post by Twisol
I'd question why you need to fill it with 10-20k individual structures per frame, personally...


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.



 User Rating: 1008   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by freeworld
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)?


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!

 User Rating: 1933   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

(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.

 User Rating: 1019   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: