why memoizing is faster

Terry Reedy tjreedy at udel.edu
Sat Mar 26 13:27:01 EDT 2011


On 3/26/2011 5:55 AM, Lie wrote:
> On Mar 26, 5:10 pm, Terry Reedy<tjre... at udel.edu>  wrote:
>> On 3/26/2011 12:17 AM, Stefan Behnel wrote:
>>
>>> 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.
>>
>> Which satisfies the time test need, but...
>>
>>   >  Future calls won't be affected.
>>
>> Good point. If one does fib(10000) and wants to dump the cache without
>> dumping all references to the function object, one can currently do
>>     fib_iter.__kwdefaults__['_cache'] = [0,1]
>> to truly reset the cache, at least with CPython.
>
> Alternatively:
>
> new_cache = [0, 1]
> fib_iter(100, new_cache)
> fib_iter(200, new_cache)

No, that is not an alternative to what I wrote ;-).
As Stefan pointed out above, passing a value to over-ride a default only 
substitutes for the default in that one call. It is like a temporary 
substitute teacher. It does not replace the default in the function 
object. The assignment I give above is like a permanent replacement 
teacher. The previous cache list will be garbage collected.

-- 
Terry Jan Reedy




More information about the Python-list mailing list