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];

(* {0.518758, Null} *)

(* {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).

  • 1
    $\begingroup$ See here for an example of using Once together with classical memoization to achieve the best of both: speed and persistence. $\endgroup$
    – Roman
    Jun 11, 2019 at 18:41

1 Answer 1


If your goal is maximum performance in a kernel session, Once is never the answer. It is just too heavyweight. It does, however, provide a real memoization method--we're still talking about sub-second evaluation, for a computation that wouldn't complete without--that is perhaps easier to read for non-experts. And if each step in the computation is expensive, the overhead of Once is negligible.

Since Once supports storing the results outside the current kernel session (e.g, local files), I could see it being used to memoize expressions where the cost of computing them is much greater than the cost of writing them to file. That way you could concievably save your memoization accross kernel restarts. I've never tried this, though.

  • 11
    $\begingroup$ Mathematica has good documentation already, but an area where it could definitely improve is explaining things like this. What is the intended use of a function? What is the workflow envisioned by its developer? Reference pages tend to be factual and don't communicate this information well (some of the examples might, but it's too easy to miss those). I would love to see more tutorial pages that explain these things. The FEM tutorial is a good example of what I'd like to see more of. $\endgroup$
    – Szabolcs
    Mar 20, 2018 at 18:31
  • 9
    $\begingroup$ The new workflow guides in 11.3 are a step in the right direction, but I'd also like to see less trivial, more in-depth tutorials, as well as an update of the old tutorials (advanced diff eq, advanced numerical integration, and especially the core of the old Mma book.) $\endgroup$
    – Szabolcs
    Mar 20, 2018 at 18:32
  • 7
    $\begingroup$ We do try to answer those questions in Applications and Properties & Relations examples. Some folks do better than others in that respect, admittedly. That said, I agree about tutorials. I'm a fan and I try to encourage others to write them. Workflows are still in an early stage. We'll see where they end up. Thanks for the feedback! $\endgroup$ Mar 20, 2018 at 21:53
  • 1
    $\begingroup$ @Szabolcs I think you found my secret reason for asking this question. $\endgroup$
    – Daniel W
    Mar 20, 2018 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.