缓冲区和Memoryview对象的非C程序员解释 [英] Buffers and Memoryview Objects explained for the non-C programmer

查看:619
本文介绍了缓冲区和Memoryview对象的非C程序员解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.7版已经推出了新的API为缓冲区和memoryview对象

Python 2.7 has introduced a new API for buffers and memoryview objects.

我看了他们的文档,我觉得我得到的基本概念(访问的原始形式对象的内部数据,而不复制它,我想指的是更快,更少的内存饥渴的方式来获得对象数据),但要真正了解文档,读者应该对C有知识超出了我有。

I read the documentation on them and I think I got the basic concept (accessing the internal data of an object in a raw form without copying it, which I suppose means a "faster and less memory-hungry" way to get object data), but to really understand the documentation, the reader should have a knowledge of C that is beyond the one I have.

我将非常感激,如果有人会花时间:

I would be very grateful if somebody would take the time to:


  • 解释浅白缓冲区和memoryview对象和

  • 描述在使用缓冲区和memoryview对象会做事的Python的方式的场景

推荐答案

下面是一个哈希函数,我写了一行:

Here's a line from a hash function I wrote:

M = tuple(buffer(M, i, Nb) for i in range(0, len(M), Nb))

这将在很长的字符串,男,分裂成较短的长度NB,其中,Nb是字节/字符我可以同时处理数'串'。它这样做是不复制字符串的任何部分,就好像我做了串片,像这样会发生:

This will split a long string, M, into shorter 'strings' of length Nb, where Nb is the number of bytes / characters I can handle at a time. It does this WITHOUT copying any parts of the string, as would happen if I made slices of the string like so:

M = tuple(M[i*Nb:i*Nb+Nb] for i in range(0, len(M), Nb))

我现在可以遍历在M就像我曾经我切片它:

I can now iterate over M just as I would had I sliced it:

H = key
for Mi in M:
    H = encrypt(H, Mi)

基本上,缓冲区和memoryviews是有效的方法来处理字符串Python中的不变性,和切片等。memoryview就像一个缓冲,但你也可以写它的一般复制行为,而不仅仅是阅读。

Basically, buffers and memoryviews are efficient ways to deal with the immutability of strings in Python, and the general copying behavior of slicing etc. A memoryview is just like a buffer, except you can also write to it, not just read.

虽然主缓冲区/ memoryview文档是关于C实现,标准类型页面有下memoryview大量资讯:的 http://docs.python.org/library/stdtypes.html#memoryview-type

While the main buffer / memoryview doc is about the C implementation, the standard types page has a bit of info under memoryview: http://docs.python.org/library/stdtypes.html#memoryview-type

编辑:在我的书签发现这个,<一个href=\"http://webcache.googleusercontent.com/search?q=cache:Ago7BXl1_qUJ:mattgattis.com/2010/3/9/python-memory-views+site:mattgattis.com+python&hl=en&client=firefox-a&gl=us&strip=1\" rel=\"nofollow\">http://webcache.googleusercontent.com/search?q=cache:Ago7BXl1_qUJ:mattgattis.com/2010/3/9/python-memory-views+site:mattgattis.com+python&hl=en&client=firefox-a&gl=us&strip=1是一个非常好的简短的归纳。

Found this in my bookmarks, http://webcache.googleusercontent.com/search?q=cache:Ago7BXl1_qUJ:mattgattis.com/2010/3/9/python-memory-views+site:mattgattis.com+python&hl=en&client=firefox-a&gl=us&strip=1 is a REALLY good brief writeup.

编辑2:原来我从该链接何时一个memoryview使用? 摆在首位,这个问题从来没有详细回答的链接已经死了,所以希望这有助于。

Edit 2: Turns out I got that link from When should a memoryview be used? in the first place, that question was never answered in detail and the link was dead, so hopefully this helps.

这篇关于缓冲区和Memoryview对象的非C程序员解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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