[SciPy-User] __sizeof__ seems wrong

David Baddeley david_baddeley at yahoo.com.au
Tue Sep 3 15:37:23 EDT 2013


Hi Gabriel,

I believe that sizeof returns the size of the 'ndarray' object (ie a pointer to the data, information about dimensions, strides, and data type etc ..), not the actual data. The full size in memory would be array.nbytes + array.__sizeof__(). The situation is, however, complicated in that the ndarray object doesn't necessarily own it's memory, and with the possibility of having multiple ndarrays pointing at the same data (ie after slicing), and it makes more sense to think of an ndarray object as being a kind of smart pointer rather than actually encompassing it's data, making the current implementation of __sizeof__ correct. One could potentially make sizeof return the full size if the array owned it's data, the 'pointer' size otherwise, but this is likely to be hard (if not impossible) to implement consistently over all possible use cases.

cheers,
David


________________________________
 From: Gabriel Gaster <gabe at gabegaster.com>
To: scipy-user at scipy.org 
Sent: Tuesday, 3 September 2013 1:18 PM
Subject: [SciPy-User] __sizeof__ seems wrong
 


This might be a bug/fix that is more appropriate for the numpy list -- but it also relates to scipy sparse...

Run the following code:

from scipy import sparse
x = sparse.rand(100,10,.2)
print x.__sizeof__()
## 32 or 16, depending
print x.data.__sizeof__()
## 80 or 40, depending
print x.data.nbytes
## 1600

import numpy
print numpy.arange(200).__sizeof__()
## 80 or 40, depending
print numpy.arange(200).nbytes

## 1600

1. Why is nbytes different than __sizeof__ ?  I believe nbytes and do not believe __sizeof__. __sizeof__ should just be rewritten to default to nbytes.
2. It seems that the __sizeof__ methods need to be updated for numpy.array AND for scipy.sparse.

I would be happy to do this -- but before I do, I wanted to sanity check with the list.
Thanks.

Gabe
_______________________________________________
SciPy-User mailing list
SciPy-User at scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20130903/e352615a/attachment.html>


More information about the SciPy-User mailing list