Lets say that i have many objects in a physics engine. New objects need to be created once in a while. What is the best way to make those new objects (and access them)?
1)Allocate each one using new
2)Allocate space for many objects, reuse space when something if something is removed (Works only if the objects are same size i guess)
3)Keep a number of free allocated spaces with room for 1 object each
With allocating space for each, youd need a buffer of pointers to go thru them. With space for many in 1 allocation you would have just a pointer or 2.
How much those differ in performance? Any other ways?
Best way to create objects dynamically?
Allocate each one using new.
Revisit issue if performance becomes a factor. Premature optimization is the root of all evil. Well, other than evil of course, I suppose evil is the root of all evil, followed thusly by premature optimization. And Will Ferrell, screw that guy.
Revisit issue if performance becomes a factor. Premature optimization is the root of all evil. Well, other than evil of course, I suppose evil is the root of all evil, followed thusly by premature optimization. And Will Ferrell, screw that guy.
I remember reading about customized dynamic allocation in the book Effective C++: 50 Specific Ways to Improve Your Programs and Design. It might be interesting for you if you need to allocate a lot of small objects, and you want to save some object overhead and CPU cycles.
I guess it would come close to your option number 2. It re-implements the new and delete operators, and if I remember correctly it allocates memory in chunks, and keeps track of live and dead objects in lists, so you get to decide when to sweep delete dead objects.
As Serapth said, this probably falls into the category of optimizations
I guess it would come close to your option number 2. It re-implements the new and delete operators, and if I remember correctly it allocates memory in chunks, and keeps track of live and dead objects in lists, so you get to decide when to sweep delete dead objects.
As Serapth said, this probably falls into the category of optimizations
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement