Most efficient way to "pre-grow" a list?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Nov 7 18:55:14 CET 2009


kj a écrit :
> 
> As I said, this is considered an optimization, at least in Perl,
> because it lets the interpreter allocate all the required memory
> in one fell swoop, instead of having to reallocate it repeatedly
> as the array grows.

IIRC, CPython has it's own way to optimize list growth.

>  (Of course, like with all optimizations,
> whether it's worth the bother is another question.)

My very humble opinion is that unless you spot a bottleneck (that is,
you have real performance issues AND the profiler identified list growth
as the culprit), the answer is a clear and obvious NO.

> Another situation where one may want to do this is if one needs to
> initialize a non-sparse array in a non-sequential order,

Then use a dict.




More information about the Python-list mailing list