Hi,
I have a question on how the size of memory malloc'd affects the
performance of a program. For example, comparing malloc-ing 100 bytes
vs 500 bytes of memory, and calling the malloc 80,000,000 times, is
the former faster than the latter since it allocates a smaller memory
block?
Thank you.
This is actually more complex than it appears. If you keep the total
amount of memory allocated fixed, and change the size of the
allocations, then the smaller allocations will have many more function
calls. For very small allocations, this overhead dominates, and the
total time taken drops inversely as the size increases.
The complexity comes in because allocators switch between allocation
algorithms at certain sizes. As the block size increases, they need
to worry more about fragmentation and excess memory usage. This
causes them to be slower than the speed-optimized small block case,
and get even slower as the block size increases. For most allocators
that I've measured on Linux, the "fastest" point is around block sizes
of 2^10 bytes plus or minus a few powers of two.
Yet another effect on allocation speed is how optimized they are with
respect to multithreading. If you have more than one thread
allocating and freeing at the same time, then some allocators can be
adversely affected.
Steven