dynamic memory allocation
I program in C++ and I was wondering if using dynamic memory allocation could cause a program to run slower even if it is used correctly.
As far as i know, it wont cause slow down to ur program. In fact, when i am doing model animation, its faster to use dynamic memory.
Working with dynamically allocated memory is not slower than working with any other memory. However, allocating and deallocating memory may have a performance impact, depending on the heap implementation.
It is generally a good idea to avoid very frequent allocations, as it will hurt performance and fragment the memory. This is a big concern in console development.
It is generally a good idea to avoid very frequent allocations, as it will hurt performance and fragment the memory. This is a big concern in console development.
As stated above, dynamic memory allocation and deallocation in languages without a garbage collector tends to be slow, so it should be avoided in high-performance situations.
Quote:Original post by mattnenterprise
I program in C++ and I was wondering if using dynamic memory allocation could cause a program to run slower even if it is used correctly.
No, the only thing that causes the program to run slower is lowering the CPU frequency.
A CPU needs a given number of cycles to perform a certain task. As such, slower or faster is problematic metric. One can talk about "cost" (how many cycles are needed to perform a certain task).
Slow can be applied in terms of FPS, but that isn't accurate metric, since in multi-threaded renderer, your FPS can remain the same, but actual rate of simulation starts dropping and can't keep up with renderer.
A "correctly" used and implemented algorithm will not cause a slowdown. It will use exactly the needed number of cycles, including dynamic allocations. These can be performed on stack, on heap, in pre-allocated buffers, in virtual memory, on a remote system, in graphics memory, ....
So when talking about "correctly implemented", the answer is simply - your algorithm may be too expensive for the given hardware.
Naive use of new/malloc however can cause algorithm to allocate memory in sub-optimal manner, causing your algorithm to be more costly than it could (or even should) be.
Memory fragmentation can make performance noticeably worse, so like Rattenhirn said, it's good to avoid frequent allocations. Searching through a fragmented heap will have overhead. One way to minimize this is to use memory pools.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement