文件

记忆功能

Call memoized function and cache results

描述

一种记忆功能object maintains the memoization semantics of a function handle and a cache of the function call results. It has the same calling syntax as the function handle specified in theFunctionproperty. However, the记忆功能object is not a function handle.

这first time you call the memoized function with a certain set of input values, MATLAB®executes the function specified by theFunctionproperty and caches the results. In later calls to the memoized function with the same set of inputs, MATLAB returns the cached results instead of executing the function again.

记忆功能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.

  1. 输入参数在数值上等于缓存的输入。比较输入值时,MATLAB对待s等于。

  2. 这number of requested output arguments matches the number of cached outputs associated with the inputs.

警告

一种记忆功能object is not aware of updates to the underlying function. If you modify the function associated with the memoized function, clear the cache with theClearCache对象功能。

创建

To create a记忆功能对象,打电话给备忘录功能。

函数的核对与输入功能相关联,而不是与输入功能相关联记忆功能目的。因此,请记住以下内容。

  • Constructing a new记忆功能对象到相同的函数创建了对相同数据的另一个引用。留下相同函数的两个变量共享缓存和对象属性值,例如缓存大小。在以下示例中,变量一种一种ndB.共享缓存并具有相同的缓存大小值。

    a = memoize(@svd);b = memoize(@svd);
    Similarly, clearing the cache forB.B.。ClearCache)也清除缓存一种, and any other variables that memoize theSVD.功能。ClearCache是A.记忆功能对象功能。

  • 分配A.记忆功能object to a new variable creates another reference to the same data. In the following example, the variablesC一种ndD.share data.

    C= memoize(@svd); d = c;

  • 清除变量不会清除与输入功能关联的缓存。清除缓存的一个记忆功能object that no longer exists in the workspace, create a new记忆功能object to the same function, and use theClearCache在新对象上的函数。或者,您可以清除所有人的缓存记忆功能objects using theclearallmemoizedCaches.功能。

特性

展开全部

记忆功能属性控制备忘录的行为。您可以访问或修改Memoized函数的属性。使用点表示法来引用特定对象和属性:

m = memoize(@ones);M.Cachesize = 25;

使用备忘录语义应用,作为函数句柄返回。此属性仅读取。

数据类型:function_handle.

最大缓存输入和输出组合数,指定为正整数。

Example:

数据类型:双倍的

缓存状态,指定为trueor错误的。To instruct MATLAB to call the function specified by theFunctionproperty regardless of whether the results are cached, and not to cache results, set this property to错误的

数据类型:逻辑

对象功能

ClearCache MemoizedFunction对象清除缓存
统计 返回MemoizedFunction对象的缓存值和统计信息

例子

展开全部

创建一个记忆功能反对通过克解D.一种tetime功能。

mf = memoize(@datetime)
MF = MemoizedFunction具有属性:功能:@DateTime启用:1缓存:10

更改缓存输入和输出组合的Maxinum数。

mf.cachesize = 2
mf = MemoizedFunction with properties: Function: @datetime Enabled: 1 CacheSize: 2

打电话给备忘录D.function with three different input values.

一种= mf('今天​​');b = mf('yesterday');c = mf('tomorrow');

打电话给统计调查缓存结果的功能。

s =统计(MF);s.cache.Inputs {:}
ANS =.1x1 cell array{'昨天'}
ANS =.1x1 cell array{'明天'}

这results of calling the memoized function with'今天​​'没有被缓存,因为缓存是2。

在当前的工作文件夹中,创建以下文件memoizequareexample.m.其中包含计算数字的平方的函数。调用该函数时,如果MATLAB返回缓存的结果,msg没有显示。

类型memoizequareexample.m.
函数m = memoizequarexample(n)m = n ^ 2;msg =“”+字符串(n)+“是”+字符串(m)+“。”;DISP(MSG)结束

弥补函数。缺省情况下,启用备忘录。

mf = memoize(@memoizeSquareExample);

用相同的输入值调用两次铭刻函数。msgis displayed only once because the second function call returns cached results.

a = mf(42);
42的平方是1764。
B = MF(42);

禁用核解并调用重复输入值的忆阻函数。虽然输入42的结果缓存,但是msg显示,因为备忘录被禁用。

mf.babled = false;C = MF(42);
42的平方是1764。

打电话给备忘录D.function with a different set of inputs.

d = MF(13);
这square of 13 is 169.

打电话给统计调查缓存结果的功能。MATLAB不会返回缓存的结果,同时禁用备忘录或收集统计信息,继续存储输入和输出值。

s = mf.stats();s.cache.Inputs {:}
ANS =.1x1 cell array{[42]}

在R2017A介绍

Was this topic helpful?