When you add an item to the list, Python is 'over-allocates', see the source code of the list object. This means that, for example, when adding 1 element to a list of 8 elements, it actually frees up space for 8 new elements and uses only the first of them. The next 7 applications are then "free."
In many languages ββ(for example, Matlab) you are always told that you need to pre-select your vectors, since adding during the cycle is very expensive. In the worst case, adding one item to a list of length n may cost O(n) time, as you may need to create a larger list and copy all existing items. You need to do this at each iteration, so the total cost of adding n elements is O(n^2) , ouch. The Python pre-distribution scheme extends the costs of array expansion across many separate applications (see amortized costs ), effectively making the cost of one append O(1) and the total cost of adding n elements O(n) .
In Python, the overhead for the rest of your code is usually so high that the slight speedup that can be obtained by pre-allocating is negligible. Therefore, in most cases, just forget about pre-allocation, unless your profiler tells you that adding to the list is a bottleneck.
Other answers show some profiling of the preallocation list itself, but this is useless. The only thing that matters is the profiling of your complete code, with all your calculations inside your loop, with and without pre-assignment. If my prediction is correct, the difference is so small that the computation time you win is overshadowed by the time spent thinking, writing and maintaining additional lines to pre-distribute your list.
Bas swinckels
source share