memoize

April 17, 2008

recently, from a discussion on the scipy-user group I discovered a very elegant way to code a function with memory.

let’s state the problem. suppose you need a function that remembers, for each time it has been called, the input parameters and its output. this can be very useful in many different situations:

  • if it is very expensive to evaluate, you can check its memory to see if it has been previously evaluated and, if yes, get its return value without actually calling it.
  • inside an optimization loop, to remember all the intermediate steps of the optimization.
  • to accumulate data on the behaviour of the function itself.
  • to surprise friends 😉

the function’s memory can be as long as the program’s running time, or it can be ‘permament’ if hard-coded in a file.

starting from here I’ve put together a couple of useful classes in python that can be used as decorators. they are use as follow:

# function to memoize
def f(x):
return x**2

# memoized function
memoized_f = memoize(f)

# memoized function on a file
memoized_persistent_f = memoize_persistent(f)

decorators can be used:

# function to memoize
@memoize # or @memoize_persistent
def f(x):
return x**2

which is equivalent to: f = memoize(f).

you can download the code here.

enjoy!

Leave a comment