efficient memoize decorator?
thattommyhallll@gmail.com
thattommyhall at gmail.com
Fri Aug 18 16:12:12 EDT 2006
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?
the code in question is as follows
"""
from memoize import memoize,memoize2
@memoize
def col(n, count):
if n == 1:
return count
elif n % 2 == 0:
return col(n/2, count+1)
else:
return col((3*n+1)/2, count+2)
import psyco
psyco.full()
start = time()
maxlength = 0
best = 0
for i in range(1, 1000001):
length = col(i,1)
if length > maxlength:
maxlength = length
best = i
print maxlength, best
end = time()
print 'took', end-start
More information about the Python-list
mailing list