memoize
Add memoization semantics to function handle
Memoizationis an optimization technique used to speed up programs by caching the results of expensive function calls and returning the cached result when the program is called with the same inputs.
Consider memoizing a function call if all of the following are true:
Performance is important.
The function is time consuming.
The function has return values that are determined entirely by the input values, and has no side effects.
System memory is adequate to store unique input and output combinations.
Syntax
memoizedFcn = memoize(fh)
Description
memoizedFcn = memoize(
adds memoization semantics to the input function handle, and returns afh
)MemoizedFunction
对象。InvokememoizedFcn
as you would invokefh
. However,memoizedFcn
is not a function handle.
TheMemoizedFunction
object maintains the cache of inputs and the corresponding outputs. When it is invoked, MATLAB®returns the associated cached output values if the following conditions are true.
The input arguments are numerically equal to cached inputs. When comparing input values, MATLAB treats
NaN
s as equal.The number of requested output arguments matches the number of cached outputs associated with the inputs.
The memoization of a function is associated with the input function and not with theMemoizedFunction
对象。因此,保持跟进ing in mind.
Constructing a new
MemoizedFunction
object to the same function creates another reference to the same data. Two variables that memoize the same function share a cache and object property values, such as cache size. In the following example, the variablesa
andb
share a cache and have the same value for cache size.a = memoize(@svd); b = memoize(@svd);
b
(b.clearCache
) also clears the cache fora
, and any other variables that memoize thesvd
function.clearCache
is aMemoizedFunction
object function.Assigning a
MemoizedFunction
object to a new variable creates another reference to the same data. In the following example, the variablesc
andd
share data.c = memoize(@svd); d = c;
Clearing a variable does not clear the cache associated with the input function. To clear the cache for a
MemoizedFunction
object that no longer exists in the workspace, create a newMemoizedFunction
object to the same function, and use theclearCache
function on the new object. Alternatively, you can clear caches for allMemoizedFunction
objects using theclearAllMemoizedCaches
function.
Caution:A |
Examples
Input Arguments
Tips
Multiple calls to
memoize
with the same function handle return the sameMemoizedFunction
对象。为example:x = memoize(@plus); y = memoize(@plus); x == y
ans = logical 1
You should not memoize a function with side effects such as setting some global state or performing I/O operations. Side effects are not repeated on subsequent calls to the memoized function with the same inputs. For example, if you memoize the
randi
function, the memoized function always returns the same value when called with the same input argument.fh = @randi; memoized_fh = memoize(fh); fh_result = [fh(100) fh(100) fh(100)] memoized_result = [memoized_fh(100) memoized_fh(100) memoized_fh(100)]
fh_result = 18 71 4 memoized_result = 28 28 28