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

标签 python python-3.x numpy python-internals internals

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

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

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

最佳答案

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

关于python - 为什么 numpy 数组有 96 字节的开销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60302874/

相关文章:

python - 将 numpy boolean 选择器限制为前几个真值的有效方法

windows - 安装 numpy 时,我的 virtualenv pip 没有读取正确的 distutils.cfg

python - 如何将函数(BigramCollocationFinder)应用于 Pandas DataFrame

python-3.x - dyld : Library not loaded:/System/Library/Frameworks/CoreFoundation.框架/版本/A/CoreFoundation

python - rpy2 rmagic 用于 ipython 将数据帧列名称中的破折号转换为点

python - django 形式的动态表

python - 在 python selenium 中单击正则表达式的链接

python - 将字节写入波形文件?

python - 堆叠 RNN 的输入形状

python - 当它不是 Django 模型时,是否可以在数据库中使用表?