473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memoization in Python

Following Antti Karttunen suggestion, I wrote the following simple decorator
for creating functions with cache (something like 'option remember' in
Maple). Just wanted to share it:

def function_with_cache(f):
def new_f(*args):
if args in new_f.cache: return new_f.cache[args]
result=f(*args)
new_f.cache[args]=result
return result
new_f.cache={}
new_f.func_name=f.func_name
return new_f

For example,

@function_with_cache
def A000045(n):
if n<2: return n
return A000045(n-1)+A000045(n-2)

A000045(3)
2

A000045.cache
{(2,): 1, (0,): 0, (3,): 2, (1,): 1}

Or, another example,

@function_with_cache
def binomial(m,n):
if m <0 or n >m: return 0
if n==0 or m==n: return 1
return binomial(m-1,n)+binomial(m-1,n-1)

binomial(5,3)
10

binomial.cache
{(3, 2): 3, (3, 3): 1, (3, 1): 3, (2, 1): 2, (2, 0): 1, (4, 3): 4, (2, 2):
1, (4, 2): 6, (1, 0): 1, (1, 1): 1, (5, 3): 10}

Alec Mihailovs
http://mihailovs.com/Alec/


Jan 5 '07 #1
2 1595
At Friday 5/1/2007 06:50, Alec Mihailovs wrote:
>Following Antti Karttunen suggestion, I wrote the following simple decorator
for creating functions with cache (something like 'option remember' in
Maple). Just wanted to share it:
Nice. There is already a memoize decorator in the Python wiki:
http://wiki.python.org/moin/PythonDecoratorLibrary, you can get some
ideas from there. Using the functools module (2.5) or the decorator
module by M. Simoniato
(http://www.phyast.pitt.edu/~micheles...mentation.html) you
can improve it so the decorated function looks more like the original.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 5 '07 #2
"Gabriel Genellina" <ga******@yahoo.com.arwrote
>
Nice. There is already a memoize decorator in the Python wiki:
http://wiki.python.org/moin/PythonDecoratorLibrary, you can get some ideas
from there. Using the functools module (2.5) or the decorator module by M.
Simoniato (http://www.phyast.pitt.edu/~micheles...mentation.html)
you can improve it so the decorated function looks more like the original.
Thank you very much. Both references are very useful.

Alec
Jan 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1365
by: Mark E. Fenner | last post by:
In the code below, the class DifferentCache utilizes three different memoization (caching) strategies. Neither the function Memoize1 or the class Memoize2 will be adequate for all three of these...
13
1407
by: Steven D'Aprano | last post by:
I was playing around with simple memoization and came up with something like this: _cache = {} def func(x): global _cache if _cache.has_key(x): return _cache else: result = x+1 # or a time...
1
1527
by: Michael Speer | last post by:
I posted this to my blog at http://michaelspeer.blogspot.com/2007/11/context-manager-for-temporary.html. I decided to forward it onto the list for comments. I thought someone might find it...
5
2779
by: trss | last post by:
Has anyone experienced automatic memoization by any C++ compiler before? The program coded as a solution for the problem based on the famous 3n +1 problem, both of which are given below, is...
7
1977
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.