Python Development, memoized functions.

Alec Mihailovs alec at mihailovs.com
Fri Jan 5 03:22:41 CET 2007


Antti,

I didn't mean to criticize your program. It certainly looks nice. And 
memoization is certainly a good thing for many purposes.

That, probably, can be done by creating a new object, something like a 
function_with_cache (including the cache attribute) instead of a function, 
and defining an instance of that object instead of defining a function.

I just wanted to say that in general, recursive programs produce a lot of 
overhead related to multiple function calls. For example, to calculate the 
millionth value, a recursive program makes (at least) two millions function 
calls. And every such call is rather expensive - it requires a lot of clock 
cycles for stack allocation, moving variables from the memory to the stack 
and back etc. In many situations that can be avoided by including this 
recursion inside a program (as I did in A000045_list example - that was just 
an example, not a procedure that I would really use for that if I had to :)

With my extensive Maple experience, I can say that it can be clearly seen in 
Maple - practically any recursive procedure, rewritten as a non-recursive 
procedure, looks longer, but works much faster.

Alec 







More information about the SeqFan mailing list