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++.
-
55As much as Python loses on benchmarks, keep in mind that that 50x or 100x slowdown is still negible if the calculation finishes in a few seconds in Python, and not even true when you do a lot of I/O or have a horrible algorithm. Rather than asking "how much slower is Python?" you should ask "is Python fast enough?" (and it most propably is, honestly) - that's also faster than benchmarking or asking here.user395760– user3957602011年01月10日 19:03:04 +00:00Commented Jan 10, 2011 at 19:03
-
4Implementing an algorithm in python is quite fast and straight forward...you simply have to do it and then check if it is fast enough. Most times you can optimize the algorithm to run much faster using different data structures(dict/sets instead of lists...) or different operations. Anyway optimization should occur after you have already implemented a first draft of the algorithm and benchmarked/profiled it.Bakuriu– Bakuriu2011年01月10日 19:09:45 +00:00Commented Jan 10, 2011 at 19:09
-
@delnan: in my case it's all about computation time. If the C variant needs x hours less, then I'd invest that time in letting the algorithms run longer/again. I simply want to find out (roughly) how much slower Python would be - if it's just a couple of hours I certainly wouldn't use a language I'm not comfortable with (you can ruin the best solutions to problems with bad implementations :P).CrazyFlyingCloseline– CrazyFlyingCloseline2011年01月10日 19:20:59 +00:00Commented Jan 10, 2011 at 19:20
-
@delnan's right about Python probably being fast enough for many things. Even when it slower, the ease of devleopment, maintenance, and future enhancement are important factors to consider.martineau– martineau2011年01月10日 19:32:28 +00:00Commented Jan 10, 2011 at 19:32
-
1Found a useful tool to convert python to c++. It helped me in converting complex python code - javainuse.com/py2cppBatman Rises– Batman Rises2022年12月06日 08:55:06 +00:00Commented Dec 6, 2022 at 8:55
8 Answers 8
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.
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.
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
4 Comments
Yes. Look at Cython. It does just that: Converts Python to C for speedups.
2 Comments
cdef declarations and thereby introduce static typing (otherwise you just juggle opaque PyObject * stuff). And it will never get quite as fast as plain C because it's usually interfacing with Python (100% or more? only for plain numerical code that doesn't interface with Python at all for the most time!). But other than that, yes, it can get you a pretty devent speedup.Shed Skin is "a (restricted) Python-to-C++ compiler".
From the docs:
Shed Skin is an experimental compiler, that can translate pure, but implicitly statically typed Python (2.4-2.6) programs into optimized C++. It can generate stand-alone programs or extension modules that can be imported and used in larger Python programs.
Besides the typing restriction, programs cannot freely use the Python standard library (although about 25 common modules, such as
randomandre, are currently supported). Also, not all Python features, such as nested functions and variable numbers of arguments, are supported.
For a set of a 75 non-trivial programs (at over 25,000 lines in total (sloccount)), measurements show a typical speedup of 2-200 times over CPython.
2 Comments
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."
2 Comments
.exe extension on OSX even though it is a perfectly normal OSX Mach-O executable. Looks like it might be a good replacement for pyinstaller, py2exe, py2app, etc. The --recurse-*** flags are important to set properly though.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.
3 Comments
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
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.
1 Comment
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..
2 Comments
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...
1 Comment
#omp parallel for or, if you wrote the inner loop in a 1 liner, and want it to be parallel too, then you can do a #omp parallel for collapse(2) and done. There's absolutely no reason to use mpi4py since Pythran releases the GIL (like Cython nogil statements) and runs your CPU at 100% with OpenMP, if you don't tell it to do otherwise. It's a great library if you can handle compiling functions in the simplest sense... I dumped Numba because of the slow JIT compile time, and AOT compiled functions don't support parallel operations.