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

Francesc Alted faltet@pytables....
Tue Nov 2 05:17:58 CDT 2010


A Monday 01 November 2010 23:15:45 Christopher Barker escrigué:
> 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.

No, you are basically right.  After the introduction of sys.getsizeof() 
I thought: hey, finally an easy way to weigh objects in Python.  But 
things are not that easy.

Sorry for quickly sending my findings without taking the time to digest 
the (strange) results more carefully.

-- 
Francesc Alted


More information about the SciPy-User mailing list