Wednesday 2 May 2012

Speedup repeated calls to Python functions

If your Python script has repeated calls to a function with the same parameters each time, you can speed things up by caching the result. This is called memoization. It's not rocket science.

What is rocket science is that with a little bit of Python magic (see the code here), you can simply add memoization to any function with the @memoized decorator, e.g.
@memoized
def calcHOMO(smiles):
   # Generate Gaussian input file with Open Babel
   # and run Gaussian to find the HOMO.
   return homo
Calling this function with a SMILES string the first time would return the HOMO after 10 minutes. Calling it a second time would return the result instantly.

Update (11/05/2012): This feature is available in the Python standard library as of Python 3.2. See Andrew's comment below

2 comments:

Andrew Dalke said...

I read in the change log that Python 3.2 even added functools.lru_cache which implements something like this as part of the standard library. It also supports a limited size cache, so you don't have to worry about your memo cache getting too large.

Too bad I'm still using Python 2.7. :)

Noel O'Boyle said...

Thanks for the info Andrew. I've updated the post.