简体   繁体   English

为什么cffi比numpy快得多?

[英]Why is cffi so much quicker than numpy?

I have been playing around with writing cffi modules in python, and their speed is making me wonder if I'm using standard python correctly. 我一直在玩python中编写cffi模块,他们的速度让我想知道我是否正确使用标准python。 It's making me want to switch to C completely! 这让我想彻底切换到C! Truthfully there are some great python libraries I could never reimplement myself in C so this is more hypothetical than anything really. 说实话,有一些伟大的python库我永远无法在C中重新实现自己,所以这比任何事情都更加假设。

This example shows the sum function in python being used with a numpy array, and how slow it is in comparison with ac function. 这个例子显示了python中的sum函数与numpy数组一起使用,以及与ac函数相比它的速度有多慢。 Is there a quicker pythonic way of computing the sum of a numpy array? 是否有更快的pythonic方法来计算numpy数组的总和?

def cast_matrix(matrix, ffi):
    ap = ffi.new("double* [%d]" % (matrix.shape[0]))
    ptr = ffi.cast("double *", matrix.ctypes.data)
    for i in range(matrix.shape[0]):
        ap[i] = ptr + i*matrix.shape[1]                                                                
    return ap 

ffi = FFI()
ffi.cdef("""
double sum(double**, int, int);
""")
C = ffi.verify("""
double sum(double** matrix,int x, int y){
    int i, j; 
    double sum = 0.0;
    for (i=0; i<x; i++){
        for (j=0; j<y; j++){
            sum = sum + matrix[i][j];
        }
    }
    return(sum);
}
""")
m = np.ones(shape=(10,10))
print 'numpy says', m.sum()

m_p = cast_matrix(m, ffi)

sm = C.sum(m_p, m.shape[0], m.shape[1])
print 'cffi says', sm

just to show the function works: 只是为了显示功能的工作原理:

numpy says 100.0
cffi says 100.0

now if I time this simple function I find that numpy is really slow! 现在,如果我计算这个简单的功能,我发现numpy真的很慢! Am I using numpy in the correct way? 我是否以正确的方式使用numpy? Is there a faster way to calculate the sum in python? 有没有更快的方法来计算python中的总和?

import time
n = 1000000

t0 = time.time()
for i in range(n): C.sum(m_p, m.shape[0], m.shape[1])
t1 = time.time()

print 'cffi', t1-t0

t0 = time.time()
for i in range(n): m.sum()
t1 = time.time()

print 'numpy', t1-t0

times: 时间:

cffi 0.818415880203
numpy 5.61657714844

Numpy is slower than C for two reasons: the Python overhead (probably similar to cffi) and generality. Numpy比C慢,原因有两个:Python开销(可能类似于cffi)和普遍性。 Numpy is designed to deal with arrays of arbitrary dimensions, in a bunch of different data types. Numpy旨在以一堆不同的数据类型处理任意维度的数组。 Your example with cffi was made for a 2D array of floats. 您的cffi示例是针对2D浮点数组生成的。 The cost was writing several lines of code vs .sum() , 6 characters to save less than 5 microseconds. 成本是编写几行代码与.sum() ,6个字符以节省不到5微秒。 (But of course, you already knew this). (但当然,你已经知道了这一点)。 I just want to emphasize that CPU time is cheap, much cheaper than developer time. 我只想强调CPU时间便宜,比开发人员时间便宜得多。

Now, if you want to stick to Numpy, and you want to get a better performance, your best option is to use Bottleneck . 现在,如果你想坚持Numpy,并希望获得更好的性能,那么你最好的选择就是使用Bottleneck They provide a few functions optimised for 1 and 2D arrays of float and doubles, and they are blazing fast. 它们提供了一些针对浮动和双打的1和2D阵列进行了优化的功能,并且它们非常快速。 In your case, 16 times faster, which will put execution time in 0.35, or about twice as fast as cffi. 在你的情况下,快16倍,这将使执行时间为0.35,或大约是cffi的两倍。

For other functions that bottleneck does not have, you can use Cython. 对于瓶颈没有的其他功能,您可以使用Cython。 It helps you write C code with a more pythonic syntax. 它可以帮助您使用更加pythonic语法编写C代码。 Or, if you will, convert progressively Python into C until you are happy with the speed. 或者,如果您愿意,可以逐步将Python转换为C,直到您对速度感到满意为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么带有 Ryzen Threadripper 的 Numpy 比 Xeon 慢这么多? - Why is Numpy with Ryzen Threadripper so much slower than Xeon? 为什么numpy.ndarray.T比numpy.transpose(numpy.ndarray)快得多? - Why is numpy.ndarray.T so much faster than numpy.transpose(numpy.ndarray)? 为什么对于2D数组乘法来说for循环比numpy更快 - Why are for loops quicker than numpy for 2D array multiplication 为什么len在DataFrame上比在底层numpy数组上效率更高? - why is len so much more efficient on DataFrame than on underlying numpy array? 在迭代NumPy数组时,为什么Cython比Numba慢得多? - Why is Cython so much slower than Numba when iterating over NumPy arrays? 为什么 numpy 平方根“x**(-1/2)”比“1/np.sqrt(x)”慢很多 - Why is numpy inverse square root "x**(-1/2)" so much slower than "1/np.sqrt(x)" 为什么在 numpy 中 log2 和 log1p 比 log 和 log10 快这么多? - Why are log2 and log1p so much faster than log and log10, in numpy? 为什么在字符串上md5哈希比在python中的numpy数组上快得多? - Why is md5 hashing so much faster on strings than on numpy arrays in python? Python Pandas:为什么numpy比Pandas的列分配要快得多? 我可以进一步优化吗? - Python Pandas: Why is numpy so much faster than Pandas for column assignment? Can I optimize further? 为什么any()比in这么快? - Why is any() so much faster than in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM