是否应在cython中定义列表项类型? [英] Should list item type be defined in cython?

查看:237
本文介绍了是否应在cython中定义列表项类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将python列表发送到cython函数以进行迭代,那么我是否想声明列表项是什么类型?另外,在cython中循环遍历列表的最佳方法是什么?例如:

If I send a python list to a cython function to iterate over, am I suppose to declare what type the list items are? Also what is the best way to loop over a list in cython? For example:

#Cython function, passed a list of float items
def cython_f(list example_list):
    cdef int i
    for i in range(len(example_list)):
        #Do stuff
        #but list item type not defined?
        pass

    #Alternative loop
    cdef j float #declaration of list item type
    for j in example_list:
        #Do stuff
        pass

尝试定义列表项类型有没有提高速度?最好传递numpy数组而不是python列表吗?

Is any speed gained from trying to define list item type? Is it preferable to pass numpy arrays instead of python lists?

推荐答案

在Cython中,您没有义务声明任何内容.通常声明类型有助于提高性能. 通常是因为如果您声明类型,但随后不使用它们,则可能会导致类型检查和打包解包.唯一可以确定的方法就是测量.

In Cython you are not obliged to declare anything. Declaring types usually helps with performance. The usually is because if you declare types, but then don't use them, you may induce type checks and pack-unpack. The only way to be sure is to measure.

要声明列表的类型,只需将其放在cdef float value的开头,并放在循环中value = example_list[i].

To declare the types of the list, just put at the beginning cdef float value, and in the loop value = example_list[i].

您应该使用list还是numpy数组?数组是统一的数据容器.这意味着您可以将其声明为float32_t,而Cython将知道如何以C速度进行操作(访问速度更快,因为它可以保证连续且在内存中步入正轨).另一方面,如果要更改大小,则最好使用列表(或者对于非常繁重的使用,可能是libcpp.vector).因此答案是,这取决于您的工作,但是在大多数情况下,数组会更好.

Should you use list or numpy array? An array is an uniform data container. This means that you can declare it as being float32_t, and Cython will know how to work with that at C speed (accessing is faster, as it is guaranteed to be contiguous and strided in memory). On the other hand, if you are going to change the size, you are probably better using lists (or for very heavy use, perhaps libcpp.vector). So the answer is it depends on what you do, but in most cases, an array is better.

为了公平起见,您必须考虑数据的生存方式.如果列表中包含所有内容,则使用数组的函数可能会更快,但是list -> array -> f_array -> array -> list可能会比list -> f_list -> list慢.如果您不在乎,通常情况下,请在长度为常数时使用数组,否则列出.另外请注意,对于大量数据,numpy数组在内存上的存储空间较小.

To be fair, you have to consider how is the data living. If you have everything in lists, your function with arrays may be faster, but list -> array -> f_array -> array -> list may be slower than list -> f_list -> list. If you don't care, as a rule of thumb, use arrays when the length will be constant and lists otherwise. Also note that numpy arrays are lighter on the memory for big amounts of data.

这篇关于是否应在cython中定义列表项类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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