MemoizedFunction

Class of the objects returned by memoize.

Category: Software Development Tools > Performance and Memory

Syntax

m = memoize(fun)

Creates an object of class matlab.lang.MemoizedFunction around fun.

m.Enabled = tf

Turns caching on (true) or off (false) for m.

m.CacheSize = n

Sets how many results the cache keeps.

Description

A MemoizedFunction object behaves like the function handle it wraps: it is called with the same arguments and returns the same outputs. In addition it owns a cache. Its properties Enabled, CacheSize and Function describe the setup, stats reports hits and misses, and clearCache empties it.

The object is a handle, so copies of the variable refer to the same cache.

Input arguments

Enabled
Logical property. When false the function is always run and nothing is stored.
CacheSize
Number of distinct input sets whose results are kept. Older entries are dropped when it is exceeded.

Output arguments

Function
Read-only handle of the wrapped function.

Example

Properties of a memoized function

m = memoize(@(x) x^2);
class(m)
m.Enabled
m.CacheSize = 5;
m.CacheSize
m.Function
Output
ans = 'matlab.lang.MemoizedFunction'
ans = logical
   1
ans = 5
ans = function_handle with value:

    @(x)x^2

See also

memoize stats clearCache clearAllMemoizedCaches