为什么numpy数组有96字节的开销?

2024-04-19 07:06:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我使用一个简单的空numpy数组,我可以看到它有96字节的开销

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

96字节存储的是什么?在numpy或Python3(cpython)的C源代码中,这是在哪里设置的


Tags: numpy字节源代码npsys数组cpythonarray
1条回答
网友
1楼 · 发布于 2024-04-19 07:06:55

数组存在于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;

相关问题 更多 >