有可能将Python程序转换为C/ c++吗?

我需要实现几个算法,我不确定性能差距是否大到足以证明我在C/ c++(我不擅长)中所经历的所有痛苦。我想写一个简单的算法,并将其与这样一个转换后的解决方案进行比较。如果仅这一点就比Python版本快得多,那么我就别无选择,只能用C/ c++来实现。


当前回答

刚在黑客新闻里看到这个新工具。

从他们的页面-“Nuitka是Python解释器的一个很好的替代品,并编译CPython 2.6, 2.7, 3.2和3.3提供的每个构造。它将Python转换为c++程序,然后使用“libpython”以与CPython相同的方式执行,以一种非常兼容的方式。”

其他回答

是的。看Cython。它所做的就是:将Python转换为C来加速。

Shed Skin是“一个(受限的)python -to- c++编译器”。

从文档中可以看出:

Shed Skin是一个实验性的编译器,它可以将纯的但隐式静态类型的Python(2.4-2.6)程序转换为优化的c++。它可以生成独立的程序或扩展模块,可以导入并在更大的Python程序中使用。

除了类型限制外,程序不能自由使用Python标准库(尽管目前支持大约25个常用模块,如random和re)。此外,并不是所有的Python特性,比如嵌套函数和可变数量的参数都被支持。

对于一组由75个非平凡程序组成的程序(总共超过25000行(sloccount)),测量结果显示,与CPython相比,典型的加速速度提高了2-200倍。

除了Shed Skin,另一种转换为c++的选择是Pythran。

引用Micha Gorelick和Ian Ozsvald的《高性能Python》:

Pythran is a Python-to-C++ compiler for a subset of Python that includes partial numpy support. It acts a little like Numba and Cython—you annotate a function’s arguments, and then it takes over with further type annotation and code specialization. It takes advantage of vectorization possibilities and of OpenMP-based parallelization possibilities. It runs using Python 2.7 only. One very interesting feature of Pythran is that it will attempt to automatically spot parallelization opportunities (e.g., if you’re using a map), and turn this into parallel code without requiring extra effort from you. You can also specify parallel sections using pragma omp > directives; in this respect, it feels very similar to Cython’s OpenMP support. Behind the scenes, Pythran will take both normal Python and numpy code and attempt to aggressively compile them into very fast C++—even faster than the results of Cython. You should note that this project is young, and you may encounter bugs; you should also note that the development team are very friendly and tend to fix bugs in a matter of hours.

如果C变体少需要x个小时,那么我会把这些时间投资在让算法运行更长时间上

“投资”这个词在这里不合适。

Build a working implementation in Python. You'll finish this long before you'd finish a C version. Measure performance with the Python profiler. Fix any problems you find. Change data structures and algorithms as necessary to really do this properly. You'll finish this long before you finish the first version in C. If it's still too slow, manually translate the well-designed and carefully constructed Python into C. Because of the way hindsight works, doing the second version from existing Python (with existing unit tests, and with existing profiling data) will still be faster than trying to do the C code from scratch.

这句话很重要。

第一次制造望远镜的汤普森规则 先做一面四英寸的镜子,再做一面六英寸的镜子,要比先做一面六英寸的镜子快。 比尔McKeenan 王研究所

我意识到一个全新的解决方案的答案缺失了。如果在代码中使用Numpy,我建议尝试Pythran:

http://pythran.readthedocs.io/

对于我尝试的函数,Pythran提供了非常好的结果。生成的函数与编写良好的Fortran代码一样快(或者只是稍微慢一点),比(相当优化的)Cython解决方案快一点。

与Cython相比,它的优点是您只需在为Numpy优化的Python函数上使用Pythran,这意味着您不必展开循环并为循环中的所有变量添加类型。Pythran会花时间分析代码,以便理解numpy.ndarray上的操作。

与Numba或其他基于即时编译的项目相比,这也是一个巨大的优势(据我所知),对于这些项目,您必须扩展循环才能真正有效。然后带有循环的代码只使用CPython和Numpy会变得非常非常低效……

Pythran的一个缺点:没有类!但是因为只有真正需要优化的函数才需要编译,所以这并不是很烦人。

另一点:Pythran很好(而且很容易)支持OpenMP并行。但是我不认为mpi4py是受支持的…