preallocate list

Bill Mill bill.mill at gmail.com
Wed Apr 13 09:11:22 EDT 2005


On 4/13/05, Jim <jbo at cannedham.ee.ed.ac.uk> wrote:
> Hi all
> 
> Is this the best way to preallocate a list of integers?
> listName = range(0,length)
> 

the 0 is unnecessary; range(length) does the same thing.

> What about non integers?
> 

arr = [myobject() for i in range(length)]

> I've just claimed in the newsgroup above that pre-allocating helps but I
> might be getting confused with matlab ;)
> 
> If I have a file with a floating point number on each line, what is the
> best way of reading them into a list (or other ordered structure)?
> 
> I was iterating with readline and appending to a list but it is taking ages.
> 

I would profile your app to see that it's your append which is taking
ages, but to preallocate a list of strings would look like:

["This is an average length string" for i in range(approx_length)]

My guess is that it won't help to preallocate, but time it and let us
know. A test to back my guess:

import timeit, math

def test1():
    lst = [0 for i in range(100000)]
    for i in xrange(100000):
        lst[i] = math.sin(i) * i

def test2():
    lst = []
    for i in xrange(100000):
        lst.append(math.sin(i) * i)

t1 = timeit.Timer('test1()', 'from __main__ import test1')
t2 = timeit.Timer('test2()', 'from __main__ import test2')
print "time1: %f" % t1.timeit(100)
print "time2: %f" % t2.timeit(100)

09:09 AM ~$ python test.py
time1: 12.435000
time2: 12.385000

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list