Trying to bench mark allocation cost

At a smaller size, like 100 elements, pre_alloc is nearly twice as fast as post_alloc, on my machine. But as the number of elements gets larger, the difference gets lost in the noise.

I believe this is because, at large sizes, the benchmark spends much more time pushing to the vector than reallocating it. At one million elements, the number of reallocations (less than twenty) is much smaller than the number of writes. And at large sizes, realloc is cheaper than you might expect. So the extra cost from needing to reallocate is too small to measure, in this specific case.

4 Likes