有可能将Python程序转换为C/ c++吗?
我需要实现几个算法,我不确定性能差距是否大到足以证明我在C/ c++(我不擅长)中所经历的所有痛苦。我想写一个简单的算法,并将其与这样一个转换后的解决方案进行比较。如果仅这一点就比Python版本快得多,那么我就别无选择,只能用C/ c++来实现。
有可能将Python程序转换为C/ c++吗?
我需要实现几个算法,我不确定性能差距是否大到足以证明我在C/ c++(我不擅长)中所经历的所有痛苦。我想写一个简单的算法,并将其与这样一个转换后的解决方案进行比较。如果仅这一点就比Python版本快得多,那么我就别无选择,只能用C/ 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倍。
如果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 王研究所
http://code.google.com/p/py2c/看起来有可能——他们也在网站上提到:Cython, Shedskin和RPython,并确认他们正在将Python代码转换为纯C/ c++,这比充斥着Python API调用的C/ c++要快得多。注意:我还没有试过,但我打算试一试。
刚在黑客新闻里看到这个新工具。
从他们的页面-“Nuitka是Python解释器的一个很好的替代品,并编译CPython 2.6, 2.7, 3.2和3.3提供的每个构造。它将Python转换为c++程序,然后使用“libpython”以与CPython相同的方式执行,以一种非常兼容的方式。”
我意识到一个全新的解决方案的答案缺失了。如果在代码中使用Numpy,我建议尝试Pythran:
http://pythran.readthedocs.io/
对于我尝试的函数,Pythran提供了非常好的结果。生成的函数与编写良好的Fortran代码一样快(或者只是稍微慢一点),比(相当优化的)Cython解决方案快一点。
与Cython相比,它的优点是您只需在为Numpy优化的Python函数上使用Pythran,这意味着您不必展开循环并为循环中的所有变量添加类型。Pythran会花时间分析代码,以便理解numpy.ndarray上的操作。
与Numba或其他基于即时编译的项目相比,这也是一个巨大的优势(据我所知),对于这些项目,您必须扩展循环才能真正有效。然后带有循环的代码只使用CPython和Numpy会变得非常非常低效……
Pythran的一个缺点:没有类!但是因为只有真正需要优化的函数才需要编译,所以这并不是很烦人。
另一点:Pythran很好(而且很容易)支持OpenMP并行。但是我不认为mpi4py是受支持的…
除了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.
我知道这是一个较老的帖子,但我想提供我认为有用的信息。
我个人使用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.
希望这能有所帮助。