If you feel "the need for speed", you may be interested in Psyco. Psyco is a Specializing Compiler not unlike Java's JIT. You write Python functions as you normally would, but for those that you want to accelerate, you pass the function to Psyco. Psyco applies its magic and returns another function reference you can use in lieu of the original. You may see speed increases of 2x, 3x, occasionally 100x, and of course, some functions actually run slower.
Psyco is 'nicer' than SWIG or Pyrex because you have less to do to get it running, and there is the potential for the JIT-like optimizations to actually be faster than typical compiled code. ...but you pay a slight penalty for the run-time compilation.
Target inner loops and stay with vanilla code. Psyco is in development, so some language features are not fully supported, and Psyco may throw an exception when a variable's data type changes mid-execution. And the current version of Psyco only supports x86 processors.
Instructions for Python 2.2.x on WindowsThese minimalist are instructions. See http://homepages.ulb.ac.be/~arigo/psyco/ for more information.
Extract the contents of this zip file to 'site-packages' in 'Lib' in your Python directory. (This .zip is only good for Windows Python v2.2.x. Source for version 0.4.0 is available here.)
Profile your code and select opportunities. Then...
For those functions you want to run under Psyco, add a line that runs them thru Psyco, as in...
pf2 = psyco.proxy(f2)
You may now use the psyco'd function in place of the original.
results = pf2(20)
Test with timing routines like...
elapsed_time = 0.0
for i in range(10000):
t0 = time.clock()
results = pf2(20)
elapsed_time += time.clock() - t0
Computational functions achieve the greatest acceleration, and those functions heavy with list and dictionary processing achieve less. (Off-topic: dictionaries are fast and can be quicker than lists in many situations.)
If you are not sure Psyco is available on the target machine, consider the following construct.
try: import psyco have_psyco = 1 except: have_psyco = 0 ... def f2(x): # <---<< no change return x*2 if have_psyco: f2 = psyco.proxy(f2) ... results = f2(20) # <---<< no change
More information is available at:
Psyco home page: http://homepages.ulb.ac.be/~arigo/psyco/
Armin's comments: psyco-ISSUES.txt
2002 European Python and Zope Conference
2002 O'Reilly Open Source Convention
Another alternative is Pyrex.