标签归档:code-generation

将Python程序转换为C / C ++代码?[关闭]

问题:将Python程序转换为C / C ++代码?[关闭]

是否可以将Python程序转换为C / C ++?

我需要实现一些算法,而且我不确定性能差距是否足够大,足以证明我在C / C ++中做的所有痛苦(我不擅长)。我考虑过要编写一个简单的算法,并针对这种转换后的解决方案进行基准测试。如果仅此一项比Python版本要快得多,那么除了在C / C ++中做到这一点,我别无选择。

is it possible to convert a Python program to 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++.


回答 0

是。看赛顿。它就是这样做的:将Python转换为C以加快速度。

Yes. Look at Cython. It does just that: Converts Python to C for speedups.


回答 1

如果C变体所需的时间减少了x个小时,那么我会花时间让算法更长或更长时间地运行

“投资”在这里不是正确的词。

  1. 用Python构建有效的实现。在完成C版本之前,您将完成此工作很久。

  2. 使用Python分析器衡量性能。解决您发现的所有问题。根据实际需要更改数据结构和算法。您需要完成很长时间才能完成C中的第一个版本。

  3. 如果仍然太慢,请手动将精心设计和精心构建的Python转换为C。

    由于事后观察的工作方式,从现有的Python(使用现有的单元测试和现有的分析数据)执行第二版仍比尝试从头开始执行C代码要快。

这句话很重要。

汤普森首次望远镜制造商的规则制作
四英寸镜然后制作六英寸镜比制造六英寸镜要快。

比尔·麦肯南·
旺研究所

If the C variant needs x hours less, then I’d invest that time in letting the algorithms run longer/again

“invest” isn’t the right word here.

  1. Build a working implementation in Python. You’ll finish this long before you’d finish a C version.

  2. 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.

  3. 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.

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


回答 2

Shed Skin是“(受限制的)Python到C ++的编译器”。

Shed Skin is “a (restricted) Python-to-C++ compiler”.


回答 3

刚刚在黑客新闻中遇到了这个新工具。

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

Just came across this new 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.”


回答 4

Pythran是除Shed Skin之外还可以转换为C ++的另一种选择。

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

Pythran是一个Python到C ++的编译器,用于部分numpy支持的Python子集。它的行为有点像Numba和Cython,您可以对函数的参数进行注释,然后由进一步的类型注释和代码专门化来接管。它利用了矢量化可能性和基于OpenMP的并行化可能性。它仅使用Python 2.7运行。

Pythran的一个非常有趣的功能是它将尝试自动发现并行化机会(例如,如果您使用map),并将其转换为并行代码,而无需您付出额外的努力。您也可以使用pragma omp >指令指定并行节;在这方面,它与Cython的OpenMP支持非常相似。

在幕后,Pythran将同时使用普通的Python和numpy代码,并试图将它们积极地编译成非常快的C ++,甚至比Cython的结果还要快。

您应该注意,这个项目还很年轻,并且可能会遇到bug。您还应该注意,开发团队非常友好,往往会在几个小时内修复错误。

Another option – to convert to C++ besides Shed Skin – is Pythran.

To quote High Performance Python by Micha Gorelick and Ian Ozsvald:

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.


回答 5

我知道这是一个较旧的主题,但是我想提供我认为是有用的信息。

我个人使用PyPy,使用pip真的很容易安装。我可以互换地使用Python / PyPy解释器,您根本不需要更改代码,我发现它比标准python解释器(Python 2x或3x)快40倍左右。我使用pyCharm Community Edition管理我的代码,我喜欢它。

我喜欢用python编写代码,因为我认为它可以使您更多地专注于任务而不是语言,这对我来说是一个巨大的优势。而且,如果您需要更快的速度,则始终可以将其编译为适用于Windows,Linux或Mac的二​​进制文件(不是直接的,但可以使用其他工具)。根据我的经验,编译时我的速度是PyPy的3.5倍,这比python快140倍。PyPy适用于Python 3x和2x代码,如果使用像PyCharm这样的IDE,则可以很容易地在PyPy,Cython和Python之间互换(尽管需要一些初始学习和设置)。

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

编辑:我想对编译做个简短的说明:编译时,生成的二进制文件比python脚本大得多,因为它在其中建立了所有依赖关系,等等。但是随后您得到了一些明显的好处:速度!,现在,该应用程序可以在没有Python或库的任何机器上运行(取决于所编译的操作系统(如果不是全部,则取决于.lol)),而无需使用Python或库,它还会使您的代码变得模糊,并且在某种程度上从技术上来说已经准备好进行生产了。一些编译器还会生成C代码,我并没有真正查看或查看它是否有用或只是乱码。祝好运。

希望有帮助。

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.

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).

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.

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.

Hope that helps.


回答 6

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

http://pythran.readthedocs.io/

对于我尝试过的功能,Pythran提供了非常好的结果。所产生的功能与已编写好的Fortran代码一样快(或稍慢),并且比(相当优化的)Cython解决方案快一点。

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

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

Pythran的缺点:没有类!但是由于只需要编译真正需要优化的功能,所以它并不是很烦人。

另一点:Pythran支持(非常容易)OpenMP并行性。但是我不支持mpi4py …

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:

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.

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.

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…

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.

Another point: Pythran supports well (and very easily) OpenMP parallelism. But I don’t think mpi4py is supported…


回答 7

http://code.google.com/p/py2c/似乎很可能-他们还在自己的网站上提到:Cython,Shedskin和RPython,并确认他们正在将Python代码转换为比C快得多的纯C / C ++。 / C ++充满了Python API调用。注意:我还没有尝试过,但是我要去做。

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