stats

Cache statistics of a memoized function.

Category: Software Development Tools > Performance and Memory

Syntax

s = stats(m)

Returns a struct describing the cache of the memoized function m.

Description

A call whose inputs were already cached is a hit, and one that had to run the function is a miss. s.Cache.TotalHits and s.Cache.TotalMisses count them, and CacheHitRatePercent is hits divided by all calls, times 100.

Use stats to check whether memoization is actually saving work, and to pick a CacheSize that fits the number of distinct inputs.

Input arguments

m
A matlab.lang.MemoizedFunction object.

Output arguments

s
Struct with Cache (stored Inputs, Outputs, HitCount, TotalHits, TotalMisses), MostHitCachedInput, CacheHitRatePercent and CacheOccupancyPercent.

Example

Hit rate

f = memoize(@(x) x^2);
f(3); f(3); f(4);
s = stats(f);
fprintf('hits %d, misses %d\n', s.Cache.TotalHits, s.Cache.TotalMisses)
fprintf('hit rate %.1f%%\n', s.CacheHitRatePercent)
Output
hits 1, misses 2
hit rate 33.3%

See also

memoize MemoizedFunction clearCache