You have to step back from your code within a minute and things abstract. What is the cost of growing a dynamic container? Programmers and researchers do not think that "it took 2 ms," but rather from the point of view of asymptotic complexity: what is the cost of growth by one element, since I already have n elements; how does this change as n increases?
If you only ever grew on a constant (or limited) amount, you periodically had to move all the data, and therefore the cost of growth would depend and increase with the size of the container. On the contrary, when you produce a container geometrically, that is, multiply its size by a fixed coefficient, each time it is filled, the expected cost of the insert does not actually depend on the number of elements, that is, a constant.
This, of course, is not always constant, but it is depreciated by constant, which means that if you continue to insert elements, then the average cost per element is constant. From time to time you have to grow and move, but these events become less and less when you insert more and more elements.
I once asked if it makes sense for C ++ distributors to grow in the way realloc does. The answers I received showed that the non-moving behavior of realloc is actually a little red herring when you think asymptotically. In the end, you will no longer be able to grow, and you will have to move, and therefore, to study the asymptotic costs, it doesnβt actually matter if realloc be non-op or not. (Moreover, immovable growth seems to upset moderate, arena-distributed distributors who expect all their distributions to be the same size.)
source share