Question

Does embedding c++ code in python using ctypes, boost.python, etc make your python application faster?

Suppose I am making an application in pygtk and I need some functions which need to be fast. So if I use c++ for certain tasks in my application will it be beneficial?

And what are other options to make python code faster?

Was it helpful?

Solution

Rewriting performance-critical parts of your code in C++ could be one option. To do this effectively, you first need to understand where the bottlenecks are. The best way to do this is probably to write everything in pure Python first, and then profile.

Another option might be to use PyPy.

Finally, if you find that the bottleneck is numerical computations, then NumPy is worth a look.

It is worth noting that if, for example, it turns out that your code is I/O-bound, then none of the above options are going to be of much help.

OTHER TIPS

It depends, there's not a definitive answer. If you write bad code in C++ it could be even slower than well written Python code.

Assuming that you can write good quality C++ code, you can expect speedups up to 20x in the performance critical parts.

As the other answer says, NumPy is a good option for numerical bottlenecks (if you think in matrix operations rather than loops!); and SciPy comes with weaver, that allows you to embed inline C++ and other goodies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top