[code.view]

[top] / python / PyMOTW / profile / profile_fibonacci_memoized.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import profile
     
     class memoize:
         # from http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
         def __init__(self, function):
             self.function = function
             self.memoized = {}
     
         def __call__(self, *args):
             try:
                 return self.memoized[args]
             except KeyError:
                 self.memoized[args] = self.function(*args)
                 return self.memoized[args]
     
     @memoize
     def fib(n):
         # from http://en.literateprograms.org/Fibonacci_numbers_(Python)
         if n == 0:
             return 0
         elif n == 1:
             return 1
         else:
             return fib(n-1) + fib(n-2)
     
     def fib_seq(n):
         seq = [ ]
         if n > 0:
             seq.extend(fib_seq(n-1))
         seq.append(fib(n))
         return seq
     
     if __name__ == '__main__':
         print 'MEMOIZED'
         print '=' * 80
         profile.run('print fib_seq(20); print')
     

[top] / python / PyMOTW / profile / profile_fibonacci_memoized.py

contact | logmethods.com