The Best Python Performance. When Speed matters…

Python is slow. There I’ve said it… I love Python but sometimes I hate it because of it’s speed. In this cases usually I code in C. But sometimes I’d just love call gcc on a python script like:

$ gcc -O3

and get a powerful fast code…

here’s a list of things I’m considering to improve the speed of my python scripts:

  • Write better code Lot’s of times it’s just a matter of correct algorithms and correct data structures…
  • psyco It’s an old (3yrs, PyPy is the successor and is faster), JIT (just in time compiler) with lot’s of memory overhead, but reliable and very easy to add to your project. Two lines with: import psyco psyco.full() can do wonders to your codes’ speed. I like it  because it’s easy to install and run (I Like simple things).
  • unladen-swallow This is a faster implementation of Python. The project is google sponsored and a branch of CPython. Thanks to Jorge Tavares to point me to this one in twitter.
  • PyPy “Rumors have it that the secret goal of PyPy is being faster-than-C which is nonsense, isn’t it?”
  • Pyrex This is a language that allows  you want to bring C data types to Python.
  • Cython Based on Pyrex, it’s a language to write C extensions to get the most out of your Python Programs…
  • Shed Skin Want C++ instead of C? Shed Skin allows you to compile Python Code (a restricted strong typed subset) to C++
  • Learn Haskell or Erlang This is just a provocation has I’m currently enamored with Haskell (still learning, still learning) and maybe will try to look at Erlang for its concurrent capabilities (I teach Concurrent Programming after all). In Haskell, compiled code is fast, not as fast as C, but fast enough to beat many languages. The functional language just turns your head inside out with some constructions but in the end I do like it a lot. And it’s useful.

If I’m forgetting something please add it to the comments. I’ll update this with new comments as I’m finding new stuff and I’ll come back to this when I’ve opted for one solution. I’m still in the shopping phase now.