Run fast code in Python

Speed is a controversial issue. Is C# faster than Java? Is C faster than C++? Is Python faster than Matlab? These questions always lead to people stating that one is faster than another, and others claiming the contrary. Some even take the trouble performing benchmarks.

Python is an interpreted language (just like Matlab), which means that rather than compiling the complete program, bits of code can executed in a running program/session. This is what makes it so suitable for scientific computing: you can very quickly run snippets of code in a sequence, or run the same piece of code repeatedly while improving it (rapid prototyping).

Unfortunately, this brings the disadvantage of speed; for-loops are interpreted and thus much slower than for-loops in C or C++. Remember though, that writing a program in C/C++ rather than Python would often cost you a considerable amount more time. Matlab and Python are about equally fast as they suffer from the same problem, but both implement functions like convolution etc. in C and will thus be as fast as they can get.

Fortunately, there are several solutions that allow you to write code that will run fast in Python: