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

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


当前回答

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

其他回答

除了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.

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

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

我知道这是一个较老的帖子,但我想提供我认为有用的信息。

我个人使用PyPy,使用pip安装非常容易。我可以互换使用Python/PyPy解释器,你根本不需要改变你的代码,我发现它大约比标准的Python解释器快40倍(Python 2x或3x)。我使用pyCharm Community Edition来管理我的代码,我喜欢它。

I like writing code in python as I think it lets you focus more on the task than the language, which is a huge plus for me. And if you need it to be even faster, you can always compile to a binary for Windows, Linux, or Mac (not straight forward but possible with other tools). From my experience, I get about 3.5x speedup over PyPy when compiling, meaning 140x faster than python. PyPy is available for Python 3x and 2x code and again if you use an IDE like PyCharm you can interchange between say PyPy, Cython, and Python very easily (takes a little of initial learning and setup though).

有些人可能会在这一点上与我争论,但我发现PyPy比Cython更快。但它们都是不错的选择。

Edit: I'd like to make another quick note about compiling: when you compile, the resulting binary is much bigger than your python script as it builds all dependencies into it, etc. But then you get a few distinct benefits: speed!, now the app will work on any machine (depending on which OS you compiled for, if not all. lol) without Python or libraries, it also obfuscates your code and is technically 'production' ready (to a degree). Some compilers also generate C code, which I haven't really looked at or seen if it's useful or just gibberish. Good luck.

希望这能有所帮助。

http://code.google.com/p/py2c/看起来有可能——他们也在网站上提到:Cython, Shedskin和RPython,并确认他们正在将Python代码转换为纯C/ c++,这比充斥着Python API调用的C/ c++要快得多。注意:我还没有试过,但我打算试一试。

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

http://pythran.readthedocs.io/

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

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

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

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

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