Python: Speed

One of the many things I like about Python is that it is fairly easy to incorporate C++ modules into it in such a way that it appears to be just another Python class. This is useful for those pieces that need to access an external library or if you want to speed up a piece of code.
For a project I did for my wife I wrote part of it in C++ using the boost python library. It's not a bad way to go since you may already need the boost libraries anyway. Another method is using the SWIG library, for a comparison of the two libraries look at this article. My experience was quite positive using the C++ with Python.
Knowing that I have this back door really feels liberating. It also means that a lot of libraries you can get for Python are actually written mostly in C or C++ and not in pure Python.
There are two other methods of speeding up your Python.
The simplest to use is Psyco which claims an average of 4x speedup (2x-100x). It works with unmodified Python code and it's only real drawback is it's consumption of memory. It's basically a type of Just-In-Time compiler.
One step up is Pyrex. Pyrex is Python with C data types, so you have to change your Python code a little bit to support Pyrex and there are a few limitations. What it comes down to, is you can take some existing Python code, add a few declarations and then use a C compiler to compile it to object code. So now you have the benefits of C's speed but get to write it in Python, very cool.


Popular posts from this blog

Shortest Sudoku solver in Python

Seven Segment Display in Inkscape

Comparing ICV to EVs to cellphones