Cython:优化本机Python内存视图

编程入门 行业动态 更新时间:2024-10-28 13:15:04
本文介绍了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内存视图

本文发布于:2023-08-05 11:15:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   本机   内存   Cython   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!