Advantage of the array module over lists?

Diez B. Roggisch deets at nospam.web.de
Sun Mar 16 19:49:33 EDT 2008


sturlamolden schrieb:
> On 13 Mar, 20:40, Tobiah <t... at tobiah.org> wrote:
>> I checked out the array module today.  It claims that
>> arrays are 'efficient'.  I figured that this must mean
>> that they are faster than lists, but this doesn't seem
>> to be the case:
>>
>> ################ one.py ##############
>> import array
>>
>> a = array.array('i')
>>
>> for x in xrange(10000000):
>>         a.append(x)
> 
> 
> Lists are better optimized for appending to the end. Python lists are
> implemented as arrays of pointers, with a few empty slots at the
> end.
> 
> Arrays are contigous memory buffers. They provide faster read-write
> access, particularly for chunks of data, but are slower at resizing.

I doubt that. AFAIK both arrays and lists are continuous memory-areas, 
that double (at least to a certain threshold or so) when reaching the 
capacity limit.

lists are of type PyObject* of course, whereas arrays are not, instead 
they are of their respective primitive type, making them more memory 
efficient.

Diez



More information about the Python-list mailing list