Why breaking down a function into multiple functions speed up runtime

In other words: if you have a vector of things for which you don’t know the type, the vector will be a vector of references to objects that have to be allocated independently in the memory. If the all the objects are of the same type, then the allocation will be of one big chunk of memory with the objects stored in packed pile in that chunk.

In the first, a lot of independent allocations may occur, one for each object of the vector. In the second, only the chunk has to be allocated, at once. But for this, the exact size (memory wise) of the objects must be known, which implies knowing their types and they having concrete types.

2 Likes