I've noticed that using the SaveDefinitions option of Manipulate makes it take a long time to evaluate.
For example, here is a notebook that uses Manipulate to show the frames from a gif (code below):
Code:
Clear["Global`"];
ims = Import["http://imgur.com/download/TbwMsiF"]; (* a 1MB gif *)
Length@ims
ByteCount[ims]
t0 = Now;
AbsoluteTiming[
Manipulate[ims[[i]],
{{i, 4, "frame"}, 1, Length@ims, 1, Appearance -> "Open"}]
]
t1 = Now;
t1 - t0
FileByteCount[NotebookFileName[]]
You can see that AbsoluteTiming claims the Manipulate call takes less than a millisecond, whereas our two calls to Now say it took half a second. Fine.
Adding SaveDefinitions makes it take almost 2 minutes, despite AbsoluteTiming claiming it ran in a millisecond (code below):
Code:
Clear["Global`"];
ims = Import["http://imgur.com/download/TbwMsiF"]; (* a 1MB gif *)
Length@ims
ByteCount[ims]
t0 = Now;
AbsoluteTiming[
Manipulate[ims[[i]],
{{i, 4, "frame"}, 1, Length@ims, 1, Appearance -> "Open"},
SaveDefinitions -> True]
]
t1 = Now;
t1 - t0
FileByteCount[NotebookFileName[]]
I'm not sure what Manipulate is doing for those 2 minutes: you can see from the pic that the whole notebook is only 3 MB.
Question 1: Why does it take so long for SaveDefinitions to save the 50 frames of the gif into 3 MB?
Question 2: Is there a simple workaround for this?
I would really like to use SaveDefinitions to make the notebook self-contained, so that I don't have to re-run the notebook every time I open it.


Withstatement, and also the{ims2,None}argument? – ConvexMartian Jun 09 '17 at 13:23With[{ims=ims},...]idiom. In your second link, you say "We have to useWithto inject source intoDynamicModule's Initialization becauseInitializationis set withRuleDelayedwhich isHoldRest." I know whatHoldRestmeans, but I don't know why it necessitates theWith[{ims=ims},...]. I searched around but "mathematica With initialization idiom" doesn't return many useful results... – ConvexMartian Jun 09 '17 at 15:34