Why does a numpy array have 96 bytes of overhead?

倖福魔咒の 提交于 2020-03-21 15:41:49

问题


If I take a simply and empty numpy array i can see it has 96 bytes of overhead,

>>> sys.getsizeof( np.array([]) )
96

What is that 96 bytes storing? Where in the C source for numpy or Python 3 (cpython) is this set up?


回答1:


Array is present in C sources in numpy/core/include/numpy/ndarraytypes.h

See: https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/ndarraytypes.h

Looks like it has several pointers, number of dimensions and PyObject_HEAD, which all may in total count to number of bytes you see.

/*                                                                                                                                                                                                                                            
 * The main array object structure.                                                                                                                                                                                                           
 */
/* This struct will be moved to a private header in a future release */
typedef struct tagPyArrayObject_fields {
    PyObject_HEAD
    /* Pointer to the raw data buffer */
    char *data;
    /* The number of dimensions, also called 'ndim' */
    int nd;
    /* The size in each dimension, also called 'shape' */
    npy_intp *dimensions;
    /*                                                                                                                                                                                                                                        
     * Number of bytes to jump to get to the                                                                                                                                                                                                  
     * next element in each dimension                                                                                                                                                                                                         
     */
    npy_intp *strides;

    PyObject *base;
    /* Pointer to type structure */
    PyArray_Descr *descr;
    /* Flags describing array -- see below */
    int flags;
    /* For weak references */
    PyObject *weakreflist;
} PyArrayObject_fields;


来源:https://stackoverflow.com/questions/60302874/why-does-a-numpy-array-have-96-bytes-of-overhead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!