简体   繁体   English

Cython在CPython + NumPy上无法提高速度

[英]No speed gain with Cython over CPython+NumPy

For a uni assignment I have written a 2D square domain flow solver in MATLAB. 对于单任务,我已经在MATLAB中编写了一个2D平方域流求解器。 To study Python I have converted the MATLAB code to Python. 为了研究Python,我已经将MATLAB代码转换为Python。 I have used NumPy to do all matrix-vector multiplications and I have used scipy.sparse.linalg.spsolve() to solve Ax=b, where A is 40x40 and sparse. 我使用NumPy进行所有矩阵向量乘法,并且使用scipy.sparse.linalg.spsolve()求解Ax = b,其中A为40x40且稀疏。

In the end I wasn't too happy about the speed of the solver. 最后,我对求解器的速度不太满意。 So I used the profiler included in Spyder to track down the bottleneck. 因此,我使用了Spyder中包含的探查器来跟踪瓶颈。 It basically turned out that all linear algebra operations are quite fast except for the system solve (using the aforementioned method). 事实证明,除了系统求解(使用上述方法)以外,所有线性代数运算都非常快。 No surprise there because solving a system is always more expensive than just multiplying some vectors and matrices. 在那里不足为奇,因为求解系统总是比仅将某些向量和矩阵相乘更为昂贵。

I turned to Cython to accelerate my solver. 我求助于Cython来加速求解器。 I read http://wiki.cython.org/tutorials/numpy and I went haywire by giving each and every variable a static type (yes I know this is not the smartest or most efficient way, but I am in a hurry to see results and will do a proper job thereafter). 我读了http://wiki.cython.org/tutorials/numpy,并通过给每个变量一个静态类型来烦恼(是的,我知道这不是最聪明或最有效的方法,但我急于看到结果,此后将做适当的工作)。 The only thing I haven't given a static type is the sparse matrix A, because it is a CSR sparse matrix and I do not yet know how to static type it. 我唯一未指定静态类型的是稀疏矩阵A,因为它是CSR稀疏矩阵,并且我还不知道如何静态键入它。 And yes I know that it is the most crucial part, because profiling showed the system solve to be the bottleneck. 是的,我知道这是最关键的部分,因为分析表明系统解决了瓶颈问题。

After finally managing to compile everything with Cython the result was exactly the same as without Cython... I do understand that the Cython performance gain would not be great because I did not tackle the bottleneck, however I do not understand why the Cython version did not run even just 1% faster. 在最终设法使用Cython编译所有内容之后,结果与没有Cython时完全相同...我确实知道Cython的性能提升不会很大,因为我没有解决瓶颈,但是我不明白为什么Cython版本会做到甚至无法运行仅1%。

Could someone please help me out with benefiting from Cython? 有人可以帮我从Cython中受益吗? How can I make my code run faster? 如何使我的代码运行更快? And how should I give a CSR sparse matrix from scipy a static type? 我应该如何从scipy中获得CSR稀疏矩阵的静态类型?

My code can be downloaded using this google drive link: https://docs.google.com/file/d/0B-nchNKLtgjeWTE4OXZrVUpfcWs/edit?usp=sharing 可以使用以下Google驱动器链接下载我的代码: https : //docs.google.com/file/d/0B-nchNKLtgjeWTE4OXZrVUpfcWs/edit?usp=sharing

Because you didn't tackle the bottleneck. 因为您没有解决瓶颈。

It sounds to me that all you have done now is to make the methods calls to NumPy a bit faster. 在我看来,您现在要做的就是使对NumPy的方法调用更快一些。 That will only help if you make a lot of calls to NumPy, but you say that that's not where the Bottleneck is for you. 仅当您多次致电NumPy时,这才有帮助,但是您说那不是瓶颈所在。

Cython enables you to speed up Python code. Cython使您能够加快Python代码的速度。 It won't help you speed up NumPy code. 它不会帮助您加快NumPy代码的速度。

Because most Numpy codes are already written in C. C codes won't benefit from Cython of course. 因为大多数Numpy代码已经用C编写。Cython当然不会使C代码受益。

If it runs too slow, you should suspect your algorithm instead. 如果运行太慢,则应该怀疑算法。

Have a look here for comparisons of different way to speed up python. 在这里看看比较不同的方法来加快python的速度。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM