什么时候应该使用内存视图?

2024-05-29 03:49:57 发布

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

可以找到memoryview的完整描述here

Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray.

A memoryview has the notion of an element, which is the atomic memory unit handled by the originating object obj. For many simple types such as bytes and bytearray, an element is a single byte, but other types such as array.array may have bigger elements.


Tags: andtheanobjsupportbytesthatis
2条回答

memoryview本质上是Python本身的一种通用NumPy数组结构(没有数学)。它允许您在数据结构(如PIL图像、SQLlite数据库、NumPy数组等)之间共享内存,而无需首先复制。这对于大型数据集非常重要。

有了它,你可以做一些事情,比如内存映射到一个非常大的文件,分割文件的一部分并对该部分进行计算(如果你使用的是NumPy,这是最简单的)。

从文档中,我认为它用于“访问支持缓冲区协议的对象的内部数据而不进行复制”,因此您可以在不填满内存的情况下处理大量数据。我不知道你是否需要例子,但很不幸,我想不出任何例子。

相关问题 更多 >

    热门问题