Re: function memoizer in Python

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Thu, 17 Mar 1994 19:20:24 -0500

A better summary of results can be had from using the
profile module. Insert 'import profile' and change all
of the "fib(n)" calls to "profile.run( 'fib('+`n`+')' )"
makes all of those "print 'fib(',n,')'" lines unnecessary.

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics

"./memo.py 15" produces:

fib( 15 ): <function fib at 2006a808>
fib( 15 )
fib( 14 )
fib( 13 )
fib( 12 )
[ 1900+ lines removed... ]
fib( 1 )
fib( 0 )
fib( 3 )
fib( 2 )
fib( 1 )
fib( 0 )
fib( 1 )
610
ncalls tottime percall cumtime percall filename:lineno(function)
1973 31.930 0.016 320.220 0.162 memo.py:31(fib)
1 0.080 0.080 32.010 32.010 <string>:0()
fib( 15 ): <method MemoizedFunc._funapply of MemoizedFunc instance at 2007a788>
fib( 15 )
fib( 14 )
fib( 13 )
fib( 12 )
fib( 11 )
fib( 10 )
fib( 9 )
fib( 8 )
fib( 7 )
fib( 6 )
fib( 5 )
fib( 4 )
fib( 3 )
fib( 2 )
fib( 1 )
fib( 0 )
610
ncalls tottime percall cumtime percall filename:lineno(function)
29 0.430 0.015 6.430 0.222 memo.py:11(_funapply)
16 0.330 0.021 6.000 0.375 memo.py:31(fib)
1 0.020 0.020 0.780 0.780 <string>:0()
fib( 16 ): <method MemoizedFunc._funapply of MemoizedFunc instance at 2007a788>
fib( 16 )
987
ncalls tottime percall cumtime percall filename:lineno(function)
3 0.040 0.013 0.080 0.027 memo.py:11(_funapply)
1 0.020 0.020 0.080 0.080 <string>:0()
1 0.020 0.020 0.040 0.040 memo.py:31(fib)
fib( 18 ): <method MemoizedFunc._funapply of MemoizedFunc instance at 2007a788>
fib( 18 )
fib( 17 )
2584
ncalls tottime percall cumtime percall filename:lineno(function)
5 0.060 0.012 0.200 0.040 memo.py:11(_funapply)
2 0.050 0.025 0.140 0.070 memo.py:31(fib)
1 0.020 0.020 0.130 0.130 <string>:0()