efficient memoize decorator?

Ziga Seilnacht ziga.seilnacht at gmail.com
Sat Aug 19 03:00:38 EDT 2006


thattommyhallll at gmail.com wrote:
> im plugging away at the problems at
> http://www.mathschallenge.net/index.php?section=project
> im trying to use them as a motivator to get into advanced topics in
> python.
> one thing that Structure And Interpretation Of Computer Programs
> teaches is that memoisation is good.
> all of the memoize decorators at the python cookbook seem to make my
> code slower.
> is using a decorator a lazy and inefficient way of doing memoization?
> can anyone point me to where would explain how to do it quickly. or is
> my function at fault?

Your problem is that you are mixing psyco and memoize decorators;
psyco cannot accelerate inner functions that use nested scopes (see
http://psyco.sourceforge.net/psycoguide/unsupported.html ).
You could try using the memoize decorator from:
http://wiki.python.org/moin/PythonDecoratorLibrary ,
which doesn't use functions with closures, or use Fredrik Lundh's
solution which puts memoization directly into the function.

Ziga




More information about the Python-list mailing list