I like using memoization (i.e. the construct myFunction[x_]:=myFunction[x]=...
) when I have a heavy function that I need to re-evaluate on the same arguments. However, I find it frustrating that each time I quit the kernel(s), all the advantage goes lost.
Is there a way of saving the results? I can think of a very cumbersome way of doing it, such as this block upon defining our function
memo = If[FileExistsQ[FileNameJoin[{Directory[], "memo.mx"}]], Import["memo.mx"], {}];
myFunction[x_] := Module[{value = ...},
AppendTo[memo, "myFunction[" <> ToString[x] <> "]=" <> ToString[value]];
Export["memo.mx", Union@memo];
myFunction[x] = value]
Evaluate[ToExpression/@memo];
Is this okay, or is there a better (or even designated) way of doing this?
DumpSave
? $\endgroup$Once
to construct persistent memoization. $\endgroup$