什么时候应该使用内存视图? [英] When should a memoryview be used?

查看:45
本文介绍了什么时候应该使用内存视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在此处找到 memoryview 的完整说明:><块引用>

创建一个引用 objmemoryview.obj 必须支持缓冲协议.支持缓冲协议的内置对象包括bytesbytearray.

memoryview 具有 element 的概念,它是由原始对象 obj 处理的原子内存单元.对于bytesbytearray等许多简单类型,一个元素是单个字节,但其他类型如array.array可能有更大的元素.

解决方案

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

这篇关于什么时候应该使用内存视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆