且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-28 19:03:58

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

有了它,您可以执行诸如将内存映射到一个非常大的文件、对该文件进行切片并对该文件进行计算(如果您使用 NumPy 最简单)之类的操作.

The full description of memoryview can be found 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.

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.

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).