将 Python 程序转换为 C/C++ 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4650243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Python program to C/C++ code?
提问by CrazyFlyingCloseline
is it possible to convert a Python program to C/C++?
是否可以将 Python 程序转换为 C/C++?
I need to implement a couple of algorithms, and I'm not sure if the performance gap is big enough to justify all the pain I'd go through when doing it in C/C++ (which I'm not good at). I thought about writing one simple algorithm and benchmark it against such a converted solution. If that alone is significantly faster than the Python version, then I'll have no other choice than doing it in C/C++.
我需要实现几个算法,我不确定性能差距是否足以证明我在 C/C++(我不擅长)中执行它时所经历的所有痛苦是合理的。我想写一个简单的算法,并根据这样一个转换后的解决方案对其进行基准测试。如果仅此一项就比 Python 版本快得多,那么除了在 C/C++ 中进行操作外,我别无选择。
采纳答案by Lennart Regebro
回答by ephemient
回答by S.Lott
If the C variant needs x hours less, then I'd invest that time in letting the algorithms run longer/again
如果 C 变体需要少 x 小时,那么我会投入时间让算法运行更长时间/再次
"invest" isn't the right word here.
“投资”在这里不是正确的词。
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.
用 Python 构建一个有效的实现。你会在完成 C 版本之前完成这个。
使用 Python 分析器测量性能。解决您发现的任何问题。根据需要更改数据结构和算法以真正正确地执行此操作。你会在完成第一个 C 版本之前完成这个。
如果还是太慢,手动将精心设计、精心构建的 Python 翻译成 C。
由于后见之明的工作方式,从现有 Python 执行第二个版本(使用现有单元测试和现有分析数据)仍然比尝试从头开始编写 C 代码要快。
This quote is important.
这句话很重要。
Thompson's Rule for First-Time Telescope Makers
It is faster to make a four-inch mirror and then a six-inch mirror than to make a six-inch mirror.Bill McKeenan
Wang Institute
Thompson 的第一次望远镜制造者规则
制作一个 4 英寸的镜子,然后制作一个 6 英寸的镜子比制作一个 6 英寸的镜子要快。Bill McKeenan
Wang 研究所
回答by ashley
http://code.google.com/p/py2c/looks like a possibility - they also mention on their site: Cython, Shedskin and RPython and confirm that they are converting Python code to pure C/C++ which is much faster than C/C++ riddled with Python API calls. Note: I haven't tried it but I am going to..
http://code.google.com/p/py2c/看起来有可能 - 他们还在他们的网站上提到:Cython、Shedskin 和 RPython 并确认他们正在将 Python 代码转换为比 C 快得多的纯 C/C++ /C++ 充满了 Python API 调用。注意:我还没有尝试过,但我要去..
回答by seagull1089
Just came across thisnew tool in hacker news.
刚刚在黑客新闻中发现了这个新工具。
From their page - "Nuitka is a good replacement for the Python interpreter and compiles every construct that CPython 2.6, 2.7, 3.2 and 3.3 offer. It translates the Python into a C++ program that then uses "libpython" to execute in the same way as CPython does, in a very compatible way."
从他们的页面 - “Nuitka 是 Python 解释器的一个很好的替代品,它编译了 CPython 2.6、2.7、3.2 和 3.3 提供的每一个构造。它将 Python 转换成一个 C++ 程序,然后使用“libpython”以相同的方式执行CPython 以一种非常兼容的方式做到了。”
回答by paugier
I realize that an answer on a quite new solution is missing. If Numpy is used in the code, I would advice to try Pythran:
我意识到缺少关于一个全新解决方案的答案。如果代码中使用了 Numpy,我建议尝试 Pythran:
http://pythran.readthedocs.io/
http://pythran.readthedocs.io/
For the functions I tried, Pythran gives extremely good results. The resulting functions are as fast as well written Fortran code (or only slightly slower) and a little bit faster than the (quite optimized) Cython solution.
对于我尝试过的功能,Pythran 给出了非常好的结果。生成的函数与编写的 Fortran 代码一样快(或仅稍微慢一点),并且比(相当优化的)Cython 解决方案快一点。
The advantage compared to Cython is that you just have to use Pythran on the Python function optimized for Numpy, meaning that you do not have to expand the loops and add types for all variables in the loop. Pythran takes its time to analyse the code so it understands the operations on numpy.ndarray.
与 Cython 相比的优势在于,您只需在针对 Numpy 优化的 Python 函数上使用 Pythran,这意味着您不必扩展循环并为循环中的所有变量添加类型。Pythran 花时间分析代码,以便了解numpy.ndarray.
It is also a huge advantage compared to Numba or other projects based on just-in-time compilation for which (to my knowledge), you have to expand the loops to be really efficient. And then the code with the loops becomes very very inefficient using only CPython and Numpy...
与 Numba 或其他基于即时编译的项目相比,这也是一个巨大的优势(据我所知),您必须扩展循环才能真正高效。然后只使用 CPython 和 Numpy 的循环代码变得非常低效......
A drawback of Pythran: no classes! But since only the functions that really need to be optimized have to be compiled, it is not very annoying.
Pythran 的一个缺点:没有类!不过既然只需要编译真正需要优化的函数,那就不是很烦了。
Another point: Pythran supports well (and very easily) OpenMP parallelism. But I don't think mpi4py is supported...
另一点:Pythran 很好地(并且非常容易地)支持 OpenMP 并行性。但我认为不支持 mpi4py ......
回答by boardrider
Another option - to convert to C++ besides Shed Skin- is Pythran.
另一种选择 - 除了Shed Skin之外转换为 C++ - 是Pythran。
To quote High Performance Python by Micha Gorelick and Ian Ozsvald:
引用Micha Gorelick 和 Ian Ozsvald 的高性能 Python 的话:
Pythran is a Python-to-C++ compiler for a subset of Python that includes partial
numpysupport. 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 usingpragma 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.
Pythran 是 Python 到 C++ 的编译器,用于包含部分
numpy支持的 Python 子集。它的作用有点像 Numba 和 Cython——你注释一个函数的参数,然后它接管进一步的类型注释和代码专门化。它利用了矢量化的可能性和基于 OpenMP 的并行化的可能性。它仅使用 Python 2.7 运行。Pythran 的一个非常有趣的特性是它会尝试自动发现并行化机会(例如,如果您正在使用
map),并将其转换为并行代码,而无需您额外的努力。您还可以使用pragma omp> 指令指定并行部分;在这方面,感觉与 Cython 的 OpenMP 支持非常相似。在幕后,Pythran 将采用普通的 Python 和 numpy 代码,并尝试将它们积极地编译成非常快的 C++——甚至比 Cython 的结果还要快。
需要注意的是,这个项目还很年轻,可能会遇到bug;您还应该注意到开发团队非常友好,往往会在几个小时内修复错误。
回答by Hymantrader
I know this is an older thread but I wanted to give what I think to be helpful information.
我知道这是一个较旧的线程,但我想提供我认为有用的信息。
I personally use PyPy which is really easy to install using pip. I interchangeably use Python/PyPy interpreter, you don't need to change your code at all and I've found it to be roughly 40x faster than the standard python interpreter (Either Python 2x or 3x). I use pyCharm Community Edition to manage my code and I love it.
我个人使用 PyPy,它使用 pip 非常容易安装。我交替使用 Python/PyPy 解释器,您根本不需要更改代码,我发现它比标准 Python 解释器(Python 2x 或 3x)快大约 40 倍。我使用 pyCharm 社区版来管理我的代码,我喜欢它。
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).
我喜欢用 python 编写代码,因为我认为它可以让你更专注于任务而不是语言,这对我来说是一个巨大的优势。如果你需要它更快,你总是可以编译为适用于 Windows、Linux 或 Mac 的二进制文件(不是直接的,但可以使用其他工具)。根据我的经验,编译时我的速度比 PyPy 快 3.5 倍,这意味着比 Python 快 140 倍。PyPy 可用于 Python 3x 和 2x 代码,如果您使用像 PyCharm 这样的 IDE,您可以非常轻松地在 PyPy、Cython 和 Python 之间进行交换(尽管需要一些初始学习和设置)。
Some people may argue with me on this one, but I find PyPy to be faster than Cython. But they're both great choices 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.
编辑:我想再做一个关于编译的快速说明:当你编译时,生成的二进制文件比你的 python 脚本大得多,因为它把所有的依赖项都构建到其中,等等。但是你会得到一些明显的好处:速度!,现在该应用程序可以在没有 Python 或库的任何机器上运行(取决于你编译的操作系统,如果不是全部。哈哈),它也会混淆你的代码,并且在技术上已经准备好(在一定程度上)“生产”。一些编译器还生成 C 代码,我没有真正看过或看到它是否有用或只是胡言乱语。祝你好运。
Hope that helps.
希望有帮助。

