Почему массив numpy имеет 96 байтов служебной информации? - PullRequest
6 голосов
/ 19 февраля 2020

Если я возьму простой и пустой массив numpy, я вижу, что он имеет 96 байтов служебной информации,

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

Что хранит эти 96 байтов? Где в источнике C для numpy или Python 3 (cpython) это настроено?

1 Ответ

7 голосов
/ 19 февраля 2020

Массив присутствует в C источниках в numpy / core / include / numpy / ndarraytypes.h

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

Похоже, что у него есть несколько указателей, количество измерений и PyObject_HEAD, которые могут в сумме подсчитать количество байтов, которые вы видите.

/*                                                                                                                                                                                                                                            
 * 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;
...