什么时候应该使用memoryview? [重复]

时间:2022-09-10 23:48:29

This question already has an answer here:

这个问题在这里已有答案:

The full description of memoryview can be found here:

memoryview的完整描述可以在这里找到:

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

创建一个引用obj的内存视图。 obj必须支持缓冲协议。支持缓冲区协议的内置对象包括bytes和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.

存储器视图具有元素的概念,该元素是由始发对象obj处理的原子存储单元。对于许多简单类型(如bytes和bytearray),元素是单个字节,但其他类型(如array.array)可能包含更大的元素。

2 个解决方案

#1


20  

A memoryview is essentially a generalized NumPy array structure in Python itself (without the math). It allows you to share memory between data-structures (things like PIL images, SQLlite data-bases, NumPy arrays, etc.) without first copying. This is very important for large data sets.

memoryview本质上是Python本身的通用NumPy数组结构(没有数学运算)。它允许您在不首次复制的情况下在数据结构(诸如PIL图像,SQLlite数据库,NumPy数组等之类)之间共享内存。这对于大型数据集非常重要。

With it you can do things like memory-map to a very large file, slice a piece of that file and do calculations on that piece (easiest if you are using NumPy).

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

#2


3  

From the documentation, I figure it's used to "access the internal data of an object that supports the buffer protocol without copying", so you can do things with huge chunks of data without filling up your memory. I don't know if you want examples, but I can't think of any, unfortunately.

从文档中,我认为它用于“访问支持缓冲协议而无需复制的对象的内部数据”,因此您可以使用大量数据来处理事务而不会填满内存。我不知道你是否想要例子,但遗憾的是我想不出任何例子。

#1


20  

A memoryview is essentially a generalized NumPy array structure in Python itself (without the math). It allows you to share memory between data-structures (things like PIL images, SQLlite data-bases, NumPy arrays, etc.) without first copying. This is very important for large data sets.

memoryview本质上是Python本身的通用NumPy数组结构(没有数学运算)。它允许您在不首次复制的情况下在数据结构(诸如PIL图像,SQLlite数据库,NumPy数组等之类)之间共享内存。这对于大型数据集非常重要。

With it you can do things like memory-map to a very large file, slice a piece of that file and do calculations on that piece (easiest if you are using NumPy).

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

#2


3  

From the documentation, I figure it's used to "access the internal data of an object that supports the buffer protocol without copying", so you can do things with huge chunks of data without filling up your memory. I don't know if you want examples, but I can't think of any, unfortunately.

从文档中,我认为它用于“访问支持缓冲协议而无需复制的对象的内部数据”,因此您可以使用大量数据来处理事务而不会填满内存。我不知道你是否想要例子,但遗憾的是我想不出任何例子。