[SciPy-User] Scipy views and slicing: Can I get a view-slice from only certain elements of an array?

Christopher Barker Chris.Barker@noaa....
Mon Nov 1 17:15:45 CDT 2010


On 10/30/10 3:04 AM, Francesc Alted wrote:
> A Saturday 30 October 2010 12:00:05 Francesc Alted escrigué:
>> NumPy arrays helps saving space too:
>>>>> sys.getsizeof(l)
>>
>> 8072
>>
>>>>> a.size*a.itemsize
>>
>> 8000   # 72 bytes less, not a lot but better than nothing
>
> Ooops.  I forgot to include the numpy headers to this.  So, probably a
> NumPy container is not more efficient (space-wise) than a plain list
> (unless you use shorter integers ;-)

hmmm -- I was surprised by this -- I always thought that numpy arrays 
were more space efficient. And, indeed, I think they are.

sys.getsizeof(a_list)

Is returning the size of the list object, which holds pointers to the 
pyobjects in the list -- so 4 bytes per object on my32 bit system.

so, if each of those objects is an int, then you need to do:

sys.getsizeof(l) + len(l)*sys.getsizeof(l[0])

and a python int is 12, rather than 4 bytes, due to the pyobject overhead.

similarly for numpy arrays:

sys.getsizeof(a) + a.size*a.itemsize

so:

 >>> l = range(1000)
 >>> sys.getsizeof(l) + len(l)*sys.getsizeof(l[0])
16036
 >>> a = numpy.arange(1000)
 >>> sys.getsizeof(a) + a.size*a.itemsize
4040

major difference (unless you are using a numpy array of objects...)

By the way: python lists over-allocate when you append, so that future 
appending can be efficient, so there is some overhead there (though not 
much, really)

Someone please correct me if I'm wrong -- I am planning on using this as 
an example in a numpy talk I'm giving to a local user group.

-Chris





-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the SciPy-User mailing list