有效地初始化Cython Memoryview [英] Initialise Cython Memoryview efficiently

查看:45
本文介绍了有效地初始化Cython Memoryview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Cython pyx 文件中设置我的 MemoryView ,如下所示:

I'm currently setting my MemoryViews in my Cython pyx file as follows:

@cython.boundscheck(False)
cdef int[:] fill_memview():
    # This happens inside a big loop so needs to be fast
    cdef int[:] x = np.empty(10)
    for i in range(10):
        x[i] = i
    return x

cdef stupid_loop():
    for i in range(10000):
        fill_memview()

当我使用 cython -a foo.pyx 编译 pyx 文件时,行 cdef int [:] x = np.empty(10)在生成的带注释的 html 文件中以深黄色显示(这意味着它有很多Python调用使速度变慢).

When I compile the pyx file with cython -a foo.pyx the line cdef int[:] x = np.empty(10) shows in the resulting annotated html file in dark yellow (meaning it has lots of Python calls slowing things down.)

如何更好地使键入的Memoryview形象化?

How can I instatiate my typed Memoryview better?

推荐答案

有关不同方法的比较,请参见此答案分配内存.如果您的需求很简单(仅索引),请特别注意"cpython.array raw C类型",则可以创建一个cpython数组以快速创建,然后使用 as_ints [i] 进行快速不安全索引,或者如果确实需要内存视图,则cpython数组上的内存视图比numpy数组快3倍.

See this answer for a comparison of different ways of allocating memory. If your needs are simple (just indexing) pay particular attention to 'cpython.array raw C type', you can create a cpython array for fast creation then use as_ints[i] for fast unsafe indexing, or if you really do need a memory view, the memory view on a cpython array is 3x faster than a numpy array.

如果不对代码的用途有更全面的了解,很难提供更具体的建议.例如,如果可能的话,最好使用二维数组,因为分配二维大数组的内存往往比分配很多小数组的效率更高,例如,制作很多小的内存视图切片会更快一个大内存视图具有大量分配的内存,而不是创建一堆小内存视图,每个视图都有自己的一小块分配的内存.

Without having a bigger picture of what your code does it's difficult to offer more specific advice. For example if possible you would be better served using a two-dimensional array as it tends to be much more efficient to allocate one large chunk of memory than lots of little ones, and for instance it's much quicker to make lots of little memory view slices of one big memory view with a big hunk of allocated memory, than to create a bunch of little memory views each with their own little piece of allocated memory.

这篇关于有效地初始化Cython Memoryview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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