【问题标题】:Why does a numpy array have 96 bytes of overhead?为什么一个 numpy 数组有 96 个字节的开销?
【发布时间】:2020-06-03 18:31:05
【问题描述】:

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

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

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

【问题讨论】:

  • 所有对象都有开销;它们不是原始值。它们存储诸如引用计数、对其类型的引用、指向存储实际数据的内存的指针等内容。
  • @chepner 我明白这一点。我什至使用了“开销”这个词。问题是这种情况下的开销是多少。 96 字节是 16、64 位整数。对于常规的 python 对象,我相信有一个 64 位 unsigned int 用于 ref 计数,一个用于打字。其他 14 个是什么?我在哪里可以找到它们?布局 numpy 数组的结构在哪里?
  • 当然,它只需要是一个具有研究证据的重点问题。您看过 Numpy 源代码了吗?
  • @chepner 是的,我有......但这绝对不是必需的。我可以问这个问题,即使我根本不知道 C,它也会完全成为主题。对这个网站而言,唯一重要的是问题的定义明确,而不是重复。
  • 而您实际上只是在请求一个指向外部资源的指针,即定义numpy.array 的源中的位置。 那让这个话题离题了。

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


【解决方案1】:

数组存在于 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 这样的语言中都存储在一个复杂的数据结构中,其内部实现只有解释器的编写者才感兴趣。 “它只是有效。就像魔法一样......” 而且,我们不得不关心诸如“96 字节”之类的事情的日子已经一去不复返了。 (耶!)
  • 我不明白您为什么要提出这一点,@MikeRobinson - 内部可能与您无关,但这当然并不意味着它们实际上无关紧要。 96 字节可能听起来不多,但有时,这就是性能好坏的区别。
猜你喜欢
  • 2020-02-06
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
相关资源
最近更新 更多