Cython:我是否应该使用np.float_t而不是double类型的内存视图 [英] Cython: Should I use np.float_t rather than double for typed memory views

查看:381
本文介绍了Cython:我是否应该使用np.float_t而不是double类型的内存视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于cython中的内存视图,如果我正在使用numpy浮点数组,那么使用NumPy类型的视图(例如np.float_t)代替简单地double进行键入是否有任何优势?

Concerning memoryviews in cython, is there any advantage of typing a view with NumPy types such as np.float_t instead of simply do double if I'm working with numpy float arrays?

我应该以相同的方式键入cdef,然后执行e. g.

And should I type the cdef then the same way, doing e. g.

ctypedef np.float64_t np_float_t
...

@cython.profile(False)
@cython.wraparound(False)
@cython.boundscheck(False)
cdef np_float_t mean_1d(np_float_t [:] v) nogil:
    cdef unsigned int n = v.shape[0]
    cdef np_float_t n_sum = 0.

    cdef Py_ssize_t i
    for i in range(n):
        n_sum += v[i]

    return n_sum / n

推荐答案

如果您查看cython随附的numpy头文件(例如,在master分支中,则为

If you look in the numpy header file included with cython (e.g. in the master branch, it is __init__.pxd), you'll find

    ctypedef double       npy_double

ctypedef npy_double     float_t

换句话说,float_t double,因此使用np.float_t不应有任何优势.

In other words, float_t is double, so there should be no advantange to using np.float_t.

这篇关于Cython:我是否应该使用np.float_t而不是double类型的内存视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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