1. Mike Graham
  2. remember

Source

remember / doc / usage.rst

Usage Guide

Remember provides decorators to cache the result of function calls. This way instead of repeatedly computing expensive results, the result of a previous computation can be used instead. Consider this simple example:

>>> import time
>>> def f(x):
...     return x ** x
...
>>>
>>> start = time.time(); result = f(500000); time.time() - start
9.4690001010894775
>>> start = time.time(); new_result = f(500000); time.time() - start
9.4839999675750732
>>> new_result == result
True

The second time takes about the same amount as the first to perform this computation, several seconds. We didn't have to spend all that time, though, since we already know the answer has to be the same as the old result. Using remember, we can cache the old values and save time:

>>> from remember.memoize import memoize
>>> @memoize(100)
... def g(x):
...     return x ** x
...
>>> start = time.time(); result = g(500000); time.time() - start
9.4690001010894775
>>> start = time.time(); new_result = g(500000); time.time() - start
0.0
>>> new_result == result
True

This time, I decorated my function with :func:`memoize` and the second time calculating the same result took very little time (so little it rounds to 0!).