array vs. list

Thomas Wouters thomas at xs4all.net
Wed Feb 14 08:37:45 EST 2001


On Fri, Feb 09, 2001 at 04:38:25PM -0700, C. Porter Bassett wrote:

> I have a couple of matrix/vector classes that I wrote.  The matrix is 4x4
> and the vector is 4X1, just like the ones they use in OpenGL.  Anyway,
> when I wrote it quick and dirty, I used all lists.  I just went back and
> replaced lists with arrays where I could.  Much to my surprise, it was
> about half again slower using arrays instead of lists!  I thought that
> arrays were supposed to be more effecient than lists, but I guess they
> aren't.  In case it would make a difference, this was done on a 400 MHZ
> linux box and python 2.0

Who said arrays would be faster ? Arrays are more efficient in storage,
because they use native arrays of native types rather than arrays of
pointers to Python objects which hold the native type (or something close to
it.) However, retrieval of a list element is a simple array index and
refcount-increment operation. Retrieval of an array element is an array
index operation and conversion to a Python object of the value found there.
This can easily trash any speed benifit you get from the better memory
footprint.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list