简体   繁体   English

如何提高Cython性能?

[英]How to improve Cython performance?

I am doing my first steps with Cython, and I am wondering how to improve performance even more. 我正在使用Cython做我的第一步,我想知道如何进一步提高性能。 Until now I got to half the usual (python only) execution time, but I think there must be more! 到目前为止,我已达到通常(仅限python)执行时间的一半,但我认为必须有更多!

I know cython -a and I already typed my variables. 我知道cython -a而且我已经输入了我的变量。 But there is still a lot in yellow in my function. 但是我的功能还有很多黄色。 Is this because cython does not recognise numpy or is there something else I am missing? 这是因为cython不识别numpy还是还有其他我缺少的东西?

I believe you can benefit by using math functions from libc as you are calling np.sqrt and np.floor on scalars. 我相信你可以通过使用libc中的数学函数来获益,因为你在scalars上调用了np.sqrt和np.floor。 This has not only the Python call overhead but there are different code paths in the numpy ufuncs for scalars and arrays. 这不仅具有Python调用开销,而且在标量和数组的numpy ufunc中存在不同的代码路径。 So that involves at least a type switch. 所以至少涉及一种类型的开关。

I think it's not a problem, as I've tested with the official tutorial , it's also reported as yellow on every np.* lines, and involves python just the same as your code. 我认为这不是问题,因为我已经使用官方教程进行了测试,它在每个np。*行上都报告为黄色,并且涉及python与您的代码相同。

Point 3 at the end of that page should have explained this: 该页末尾的第3点应解释如下:

Calling NumPy/SciPy functions currently has a Python call overhead; 调用NumPy / SciPy函数目前有一个Python调用开销; it would be possible to take a short-cut from Cython directly to C. (This does however require some isolated and incremental changes to those libraries; mail the Cython mailing list for details). 可以直接从Cython中获取C语言的快捷方式(但这确实需要对这些库进行一些隔离和增量更改;请发送Cython邮件列表以获取详细信息)。

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

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