Speeding up python code

From QMC

Jump to: navigation, search

Python is very expressive (you can do a lot in few lines of code) and very pedagogical (by design). On the flip side, it can also be very slow. For some things, this is not a problem, although there are many places in computational physics where it becomes one. Some of this problem has been resolved by having good matrix libraries that call C functions. Nonetheless, for tightly running "loops" that have many function calls and array accesses it tends to be very bad because

  1. it does bounds checking and so has extra work for each array access (certain appropriately written array access can only check ranges) and
  2. function calls can't be inlined

I have found that a typical computational physics code can run anywhere from 10x (if you're being careful and effecient) to 500x slower (if you are writing pretty but slow code).


The received wisdom for how to deal with this speed problem is to rewrite it in C++. (About this, I am only half joking). What is typically considered prudent, is to find the parts of the code that are especially slow and rewrite those in C++. This allows for most of the code to take advantage of the beauty of python while still ensuring reasonable speed. There are a myriad of ways of gluing python with fortran (f2py) and C (swig, weave.inline) and C++ (boost) although none of them are the single unique answer.

This is what we've done here. We've rewritten the VMC code (which is essentially a small loop) in C++ that is being called from python. You may notice that it's an order of magnitude or two faster. This allows us to keep some of the more cumbersome optimization methods in a scripting language like python.

Python does supply a convenient tool to help profile your code called hotshot that helps you significantly in this endavour. Donald Knuth once said that optimization is the root of all evil and you certainly shouldn't worry about speeding things up too much until you're confident of what's going slowly.

Personal tools