Cython:优化本机Python内存视图 [英] Cython: optimize native Python memoryview

查看:143
本文介绍了Cython:优化本机Python内存视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数(来自外部Python库),该函数返回要在Cython中处理的 memoryview 对象。

I have a function (from an external Python library) that returns a memoryview object that I want to process in Cython.

有没有一种方法可以将其转换为输入的字节存储视图(无副本)以提高效率?我该怎么办?这不起作用:

Is there a way to convert it to a typed memoryview of bytes (without copy) for efficiency? And how would I do that? This doesn't work:

mv = memoryview(b'1234')
cdef char[:] *tmv
tmv = mv
     ^
------------------------------------------------------------

/home/me/.cache/ipython/cython/_cython_magic_f9f608355444e2d6828580906623cea8.pyx:3:6: Cannot convert Python object to '__Pyx_memviewslice *'


推荐答案

由@CodeSurgeon链接的答案是可行的。但是,由于Cython 0.28,我们有了一种更简洁的方法-键入只读内存视图:

This answer linked by @CodeSurgeon is a possibility to do it. However, since Cython 0.28 we have a much cleaner way - the typed read-only memoryviews:

%%cython
mv = memoryview(b'1234')
cdef const unsigned char[:] tmv=mv  #"const" is possible since Cython 0.28

很显然,您只能从此内存视图读取(这是一件好事),并且不涉及复制。

Obviously you can only read from this memoryview (this is a Good Thing) and there is no copying involved.

您还可以说:但这是未签名的字符,而不是 char !没错-这也是一件好事: bytes unsigned char s,是您所键入的内存视图的全部内容

You could also say: but this is unsigned char and not char! True - this is also a Good Thing: bytes are unsigned chars and the whole point of typed memoryviews that you aren't mixing up the types!

我认为链接解决方案有点危险的另一个原因-您拥有C的全部能力来射击自己,因为它丢弃类型和常量。例如,查看:

Another reason I think the linked solution is kind of dangerous - you have the whole power of C to shoot yourself in the foot, because it casts types and constness away. See for example:

%%cython    
def bad_things(a):
   cdef const unsigned char[:] safe=a
   cdef char *unsafe=<char *> &safe[0] #who needs const and types anyway?
   unsafe[0]=52   #replace through `4`

现在:

 >>> A=b'a'
 >>> bad_things(A)
 >>> print(A)   #take it Python - I pwned your immutability!
 b'4'
 >>> print(b'a')
 b'4'
 >>> #Help! What are you doing Python?

因为Python有一个小的字符串池,这些字符串是不可变的(或者Python认为如此),并且我们已将 b'a'链接到的对象更改为 b'4'的对象,我们应该为搞笑做好准备结果和调试愉快...

Because Python has a pool of small strings, which are immutable (or so the Python thinks), and we have changed the object to which b'a' is linked to b'4' we should brace ourself for funny results and happy debugging...

总的来说,这很容易:坚持使用类型化的内存视图来保证类型和常量安全性。

In overall, this is a no-brainer: stick to typed memoryviews which guarantee type- and constness-safety.

这篇关于Cython:优化本机Python内存视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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