why memoizing is faster
Terry Reedy
tjreedy at udel.edu
Sat Mar 26 13:35:46 EDT 2011
On 3/26/2011 7:06 AM, Andrea Crotti wrote:
> Stefan Behnel<stefan_ml at behnel.de> writes:
>>
>> Not "restarted" in the sense that it gets cleaned up, though. The
>> above simply passes an explicit value for it that will be used for the
>> single call. Future calls won't be affected.
>>
>> Stefan
>
> About this global caching thing I thought, but isn't this a source of
> possible HUGE memory leaks?
The original decorator that used one dictionary for possibly caching
multiple functions could certainly cause memory problems. It would not
disappear until the decorator and all wrapped functions were gone.
> I mean, when is the object _cache freed from the memory?
> From my understanding until the function name is in the scope that
> object will never be freed, is that correct?
You mean 'while the function name...'. And yes, correct, unless one used
the trick I posted in response to Stefen:
fib_iter.__kwdefaults__['_cache'] = [0,1]
The point of this might be to free a cache swollen by a one-time call
with a large n, without deleting the function itself. One could then
continue with calls using normal-sized ints.
Caching is a time-space tradeoff that might need tuning to a particular
application.
--
Terry Jan Reedy
More information about the Python-list
mailing list