Tibor's Musings

Python Psyco

A simple (and sometimes very efficient) way to speed up your Python programs is via the Psyco module. But beware, Psyco only runs on 32-bit OSes.

Basic Psyco usage is very simple: just do:

import psyco

and later:


for each function/class you want to speed up. Benefits depend a lot on the nature of your problem: psyco helps mainly with simple code logic and cycles like for i in foo, and does not help much (or may slow things down) with complex code logic.

Simple code logic example

In my experience, naked Python is typically 5x to 50x slower than Common Lisp (CMUCL). Similar numbers are cited in Peter Norvig's Python for Lisp Programmers. With simple code logic like many for cycles, Psyco can provide very significant speed improvement (2x-20x, in my experience), bringing the difference between Python and Common Lisp down to the same order of magnitude, say 2x-5x. For example, for the Fibonacci numbers program from the Great Computer Language Shootout, if I add to the Python source:

import psyco

I obtain, for fib(37)=39088169, practically the CMUCL time:

Python raw ....... 77.71 sec
Python + Psyco ...  1.63 sec
CMUCL ............  1.42 sec
OCaml ............  0.42 sec

Just to illustrate where one can get when the bottlenecks in the Python source code are well "psycoable".

Complex code logic example

For cases with complex code logic (not even speaking of including database connections and stuff) I have observed little help from Psyco. It could even slow things down.

Each code candidate for speed-up is to be tested on its "psycoability".