Memory pre-allocation for large lists

Peter Otten __peter__ at web.de
Wed Feb 18 08:33:26 EST 2004


Jeremy Fincher wrote:

> functor% python /usr/lib/python2.3/timeit.py 'L = [None]*1000' 'for _
> in xrange(1000): pass'
> 1000 loops, best of 3: 248 usec per loop
> functor% python /usr/lib/python2.3/timeit.py 'L = []' 'for _ in
> xrange(1000): L.append(None)'
> 1000 loops, best of 3: 1.88e+03 usec per loop
> 
> So there's a factor of 7.5 difference in speed there.  I doubt it
> matters, but it at least lends some credibility to the idea that
> preallocation might be useful.

In the real world, (some of) the default values have to be overwritten,
which reduces the speed advantage to a factor of 2:

$ timeit.py 'L=[None]*1000' 'for i in xrange(1000): L[i] = None'
1000 loops, best of 3: 338 usec per loop
$ timeit.py 'L=[]' 'for i in xrange(1000): L.append(None)'
1000 loops, best of 3: 660 usec per loop

Peter



More information about the Python-list mailing list