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

问题描述 投票:3回答:1

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

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

96个字节存储了什么?在numpy或Python 3(cpython)的C源代码中的哪里进行设置?

python python-3.x numpy python-internals internals
1个回答
6
投票

数组在numpy / core / include / numpy / ndarraytypes.h]中的C源代码中存在]

参见: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;
© www.soinside.com 2019 - 2024. All rights reserved.