c++ optimizations
does anyone know of any guide which goes through which c++ functions/operators are ok to use often and which should be avoided unless absolutely necessary?
ok, if you don''t, can you tell me how much overhead new and delete (or malloc and free) create?
at the moment i''m using new and delete to create and dispose of some memory blocks every frame, and i''m wondering if it''s really worth allocating them right at the beginning, then freeing them at the end, of if it doesn''t really matter very much?
Since no-one else has replied yet, I will.
The new and delete methods are really quite fast. But from what you were saying, you performed the operations each frame. Depending on what you are doing, this may be bad.
Consider for a moment that you are allocating 100-pointers with new and deallocating them with delete every frame. Assume for sake of arguments here that each allocation/deallocation (block of 100) takes roughly 1-ms each so you have 2-ms each frame wasted. For 30-fps, that''s 60-ms wasted.
I don''t know about you, but I think that those 60-ms would be better used by applying it to AI instead of memory allocations.
Regards,
Jumpster
The new and delete methods are really quite fast. But from what you were saying, you performed the operations each frame. Depending on what you are doing, this may be bad.
Consider for a moment that you are allocating 100-pointers with new and deallocating them with delete every frame. Assume for sake of arguments here that each allocation/deallocation (block of 100) takes roughly 1-ms each so you have 2-ms each frame wasted. For 30-fps, that''s 60-ms wasted.
I don''t know about you, but I think that those 60-ms would be better used by applying it to AI instead of memory allocations.
Regards,
Jumpster
Regards,JumpsterSemper Fi
Do you have any actual numbers on if or how much slower new and delete are? It might not really be a big deal. And if you need %insert large number here% allocations per frame you could allocate it from a pool of already pre-allocated memory.
yeah i get the impression that new and delete are pretty fast.
the reason i ask is that the CModel class in our game (for loading/displaying 3d models) has some in-betweening code, so that you can specify it to draw frame 3.25, or whatever - i think you get the idea.
we''re doing this at the moment by allocating a new vertex array and filling that with the interpolated vertex values, and then deleting it when we''re done - so my question is, is it worth having a pre-allocated (when the game is started up) array to temporarily store the interpolated values in, or is it ok just to allocate/deallocate every frame?
it''s the old memory-speed tradeoff, as always!
anyway i find it hard to believe that 100 allocs takes 1-ms.
steffan - what would be the point of pre-allocating the data, then having a second allocation mechanism on top of that? i doubt i could make my functions as fast as microsoft''s
anyway, does anyone know of a site that gives a guide to optimizing your c++ code, with a wider scope than just new/delete?
the reason i ask is that the CModel class in our game (for loading/displaying 3d models) has some in-betweening code, so that you can specify it to draw frame 3.25, or whatever - i think you get the idea.
we''re doing this at the moment by allocating a new vertex array and filling that with the interpolated vertex values, and then deleting it when we''re done - so my question is, is it worth having a pre-allocated (when the game is started up) array to temporarily store the interpolated values in, or is it ok just to allocate/deallocate every frame?
it''s the old memory-speed tradeoff, as always!
anyway i find it hard to believe that 100 allocs takes 1-ms.
steffan - what would be the point of pre-allocating the data, then having a second allocation mechanism on top of that? i doubt i could make my functions as fast as microsoft''s
anyway, does anyone know of a site that gives a guide to optimizing your c++ code, with a wider scope than just new/delete?
Yup. I do. It''s a very interesting, and quite intelligent article all about that topic, and it gives you a lot of insight on what is quicker and more efficient, especially when dealing with classes. Here it is:
C++ Optimization Strategies
http://www.tantalon.com/pete/cppopt/main.htm
Hope that helps.
farmersckn
C++ Optimization Strategies
http://www.tantalon.com/pete/cppopt/main.htm
Hope that helps.
farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
quote:
anyway i find it hard to believe that 100 allocs takes 1-ms.
You're probably right but I was just throwing out a number as an example... "Assume for sake of arguments here that each allocation/deallocation (block of 100) takes roughly 1-ms...".
Regards,
Jumpster
Edited by - Jumpster on December 11, 2000 7:17:54 PM
Regards,JumpsterSemper Fi
quote: Original post by remo
yeah i get the impression that new and delete are pretty fast.
the reason i ask is that the CModel class in our game (for loading/displaying 3d models) has some in-betweening code, so that you can specify it to draw frame 3.25, or whatever - i think you get the idea.
we''re doing this at the moment by allocating a new vertex array and filling that with the interpolated vertex values, and then deleting it when we''re done - so my question is, is it worth having a pre-allocated (when the game is started up) array to temporarily store the interpolated values in, or is it ok just to allocate/deallocate every frame?
it''s the old memory-speed tradeoff, as always!
anyway i find it hard to believe that 100 allocs takes 1-ms.
steffan - what would be the point of pre-allocating the data, then having a second allocation mechanism on top of that? i doubt i could make my functions as fast as microsoft''s
anyway, does anyone know of a site that gives a guide to optimizing your c++ code, with a wider scope than just new/delete?
It can prove to be a great speed increase, trust me. As for the second allocation mechanism, it is always good to have a generic interface, preferably a stl allocator.
There are some example code on flipcode under the "code of the day" archives.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement