While watching Stephen Wolfram's twitch stream I saw him use Once
for memoization, which surprised me since I thought of Once
as a kind of general purpose Needs
. It is quite slow compared to the usual memoization pattern
f[0] = f[1] = 1;
f[x_] := Once[f[x - 1] + f[x - 2]];
g[0] = g[1] = 1;
g[x_] := g[x] = g[x - 1] + g[x - 2];
Timing[f[1000];]
(* {0.518758, Null} *)
Timing[g[1000];]
(* {0.003621, Null} *)
However, I was wondering if anyone else has found situations where this method is useful, perhaps for memoization with a persistence beyond the current kernel session (using the ExpirationDate
or PersistenceTime
options).
Once
together with classical memoization to achieve the best of both: speed and persistence. $\endgroup$