优化与C扩展或用Cython不平凡的Python应用程序教程 [英] Tutorials on optimizing non-trivial Python applications with C extensions or Cython

查看:215
本文介绍了优化与C扩展或用Cython不平凡的Python应用程序教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python社区已经出版展示如何分析的Python code和Python扩展的C或在用Cython 。我仍然在寻找教程这说明,但是,对于非平凡的Python程序,如下:

The Python community has published helpful reference material showing how to profile Python code, and the technical details of Python extensions in C or in Cython. I am still searching for tutorials which show, however, for non-trivial Python programs, the following:


  1. 如何确定哪些将通过优化受益的转换到C扩展的热点

  2. 同样重要的是,如何识别热点这将的的受益转换为C扩展

  3. 最后,如何在Python到C适当的转换,可以使用Python C-API或者使用用Cython。
  4. (甚至preferably)
  1. How to identify the hotspots which will benefit from optimization by conversion to a C extension
  2. Just as importantly, how to identify the hotspots which will not benefit from conversion to a C extension
  3. Finally, how to make the appropriate conversion from Python to C, either using the Python C-API or (perhaps even preferably) using Cython.

有一个很好的教程就为读者提供了如何通过一个完整的例子致力于通过优化的问题原因的方法。我有没有成功找到这样的资源。

A good tutorial would provide the reader with a methodology on how to reason through the problem of optimization by working through a complete example. I have had no success finding such a resource.

你知道的(或者你写的)这样一个教程?

有关澄清,我在覆盖的只有的以下教程不感兴趣:

For clarification, I'm not interested in tutorials that cover only the following:

  • Using (c)Profile to profile Python code to measure running times
  • Using tools to examine profiles (I recommend RunSnakeRun)
  • Optimizing by selecting more appropriate algorithms or Python constructs (e.g., sets for membership tests instead of lists); the tutorial should assume the algorithm and Python code is already optimal, and we are at a point where a C extension is the next logical step
  • Recapitulating the Python documentation on writing C extensions, which is already excellent as a reference but not useful as a resource for showing when and how to move from Python to C.

推荐答案

点1和2是大拇指的只是基本的优化规则。如果有任何地方教程你正在寻找的那种我会非常惊讶。也许这就是为什么你还没有找到一个。我的短名单:

Points 1 and 2 are just basic optimization rule of thumbs. I would be very astonished if there was anywhere the kind of tutorial you are looking for. Maybe that's why you haven't found one. My short list:


  • 优化的规则号之一的不要

  • 规则二的措施

  • 规则三的确定的限制因素(如果它的IO或数据库约束,没有优化可能到达反正)。

  • 规则序号四是的认为 用更好的算法和数据结构
    ...

  • 考虑语言的变化是相当低的就行了...

  • rule number one of optimization is don't.
  • rule number two measure
  • rule number three identify the limiting factor (if it's IO or database bound, no optimization may be reachable anyway).
  • rule number four is think, use better algorithms and data structure ...
  • considering a change of language is quite low on the list...

仅仅通过分析你的Python code。与普通的Python工具的开始。找到您code需要在哪里进行优化。然后尝试优化它与Python坚持。如果仍然太慢,试着去了解为什么。如果它的IO约束很可能一个C程序会更好。如果问题从算法来它也不可能将Ç更好的表现。真的是好的情况下,其中C可以帮助是非常罕见的,运行时不应该太远离你想要的东西(如2 3的时候加速比)数据结构是simples,并会从低水平重复presentation受益你真的,真的需要加速。在使用,而不是蟒蛇Ç其他大多数情况下将是一个吃力不讨好的工作。

Just start by profiling your python code with usual python tools. Find where you code need to be optimized. Then try to optimize it sticking with python. If it is still too slow, try to understand why. If it's IO bound it is unlikely a C program would be better. If the problem come from the algorithm it is also unlikely C would perform better. Really the "good" cases where C could help are quite rare, runtime should not be too far away from what you want (like a 2 of 3 times speedup) data structure are simples and would benefit from a low level representation and you really, really need that speedup. In most other cases using C instead of python will be an unrewarding job.

从蟒蛇真的是相当罕见的调用C code与性能考虑做作为首要目标。更多的时候我们的目标是蟒蛇接口与一些现有的C code。

Really it is quite rare calling C code from python is done with performance in mind as a primary goal. More often the goal is to interface python with some existing C code.

而作为另一其他的海报说,你可能会得到更好的建议使用用Cython的。

And as another other poster said, you would probably be better advised of using cython.

如果您仍然希望写为Python C模块中,所有必要的是在官方文档。

If you still want to write a C module for Python, all necessary is in the official documentation.

这篇关于优化与C扩展或用Cython不平凡的Python应用程序教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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