(Taken almost verbatim from the psyco introduction!)
psyco is a specializing compiler that lets you run your existing Python code much faster, with absolutely no change in your source code. It acts like a just-in-time compiler by rewriting several versions of your code blocks and then optimizing them by specializing the variables they use.
The main benefit is that you get a 2-100x speed-up with an unmodified Python interpreter and unmodified source code. (You just need to import psyco.)
The main drawbacks are that it only runs on i386-compatible processors (so, not PPC Macs) and it’s a bit of a memory hog.
For example, if you use the prime number generator generator code (see Idiomatic Python) to generate all primes under 100000, it takes about 10.4 seconds on my development server. With psyco, it takes about 1.6 seconds (that’s about a 6x speedup). Even when doing less numerical stuff, I see at least a 2x speedup.
Installing psyco¶
(Note: psyco is an extension module and does not come in pre-compiled form. Therefore, you will need to have a Python-compatible C compiler installed in order to install psyco.)
Grab the latest psyco snapshot from here:
http://psyco.sourceforge.net/psycoguide/sources.html
unpack it, and run ‘python setup.py install’.
Using psyco¶
Put the following code at the top of your __main__ Python script:
try:
import psyco
psyco.full()
except ImportError:
pass
...and you’re done. (Yes, it’s magic!)
The only place where psyco won’t help you much is when you have already recoded the CPU-intensive component of your code into an extension module.