| Home | Trees | Indices | Help |
|---|
|
|
object --+
|
memoize
Decorator to use automatic memoization on a given function, such
that results are chached and, if called a second time, the
function will return the cached value. Example::
@memoize
def fibonacci(n):
print "Computing fibonacci of:", n
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)
fibonacci(5)
If we removed the @memoize decorator, it would print O(2^n) lines
instead showing a exponential degeneration due to the binary
recursion. However, already computed values will not recurse,
thus this program will print on the console:
Computing fibonacci of: 5
Computing fibonacci of: 4
Computing fibonacci of: 3
Computing fibonacci of: 2
Computing fibonacci of: 1
Note that every computed value is cached in global state, so this
can be inapropiate when the function domain is very big.
|
|||
|
|||
|
|||
|
Inherited from |
|||
|
|||
|
Inherited from |
|||
|
|||
x.__init__(...) initializes x; see help(type(x)) for signature
|
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Wed Mar 6 18:53:32 2013 | http://epydoc.sourceforge.net |