Search code examples
c++pythoncctypesembedding

Does embedding c++ code in python make your python application faster?


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?


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.