Trying Cython for the first time, trying to get a speedup on a function that does subtraction and addition on 2 numpy arrays and a float32. I'm trying to get this function to be as quick as possible it's called a lot of times and if i can speed this up then it's a big win.
第一次尝试Cython,尝试获得对两个numpy数组和一个float32进行减法和加法的函数的加速。我试着让这个函数尽可能快它被调用了很多次如果我能加速它,那就是一个大胜利。
def broadcast(w, m, spl):
"""
w and m are float32 ndarrays e.g shape (43,)
spl is an nd.float32 value e.g 9722.0
"""
return w + (m - spl)
My cythonising so far is
到目前为止,我的兴奋是
import numpy as np
cimport numpy as np
DTYPE = np.float32
ctypedef np.float32_t DTYPE_t
def broadcast(np.ndarray w, np.ndarray m, np.float32 spl):
return w + (m - spl)
but it returns the error:
但它返回错误:
'float32' is not a type identifier
I'm not sure how why I can't declare the type? Do I need to declare a C type? What is an np.float32 in C?
我不知道为什么我不能声明类型?我需要声明C类型吗?什么是一个np。在C float32吗?
1 个解决方案
#1
4
As commented by @YXD, you won't get a speed improvement using Cython to loop over the arrays to perform these operations. Specially for simple operations, Numpy uses SIMD programming which is very efficient.
正如@YXD所评论的那样,使用Cython对数组进行循环来执行这些操作,您不会得到一个速度改进。特别是对于简单的操作,Numpy使用SIMD编程,这是非常有效的。
Despite of that, you could improve the memory usage and the performance by modifying array w
in place, if the original array is no longer needed:
尽管如此,如果不再需要原始数组,您可以通过修改适当的数组w来提高内存使用和性能:
def func(w, m, spl)
w += m
w -= spl
then, instead of calling:
然后,而不是调用:
out = func(w, m, spl)
the new call:
新电话:
func(w, m, spl)
will store the output inside w
.
将输出存储在w中。
#1
4
As commented by @YXD, you won't get a speed improvement using Cython to loop over the arrays to perform these operations. Specially for simple operations, Numpy uses SIMD programming which is very efficient.
正如@YXD所评论的那样,使用Cython对数组进行循环来执行这些操作,您不会得到一个速度改进。特别是对于简单的操作,Numpy使用SIMD编程,这是非常有效的。
Despite of that, you could improve the memory usage and the performance by modifying array w
in place, if the original array is no longer needed:
尽管如此,如果不再需要原始数组,您可以通过修改适当的数组w来提高内存使用和性能:
def func(w, m, spl)
w += m
w -= spl
then, instead of calling:
然后,而不是调用:
out = func(w, m, spl)
the new call:
新电话:
func(w, m, spl)
will store the output inside w
.
将输出存储在w中。