I would like to ask if anyone knows about any problems (performance or others) if someone had to identify / put the modules (modules) used by the Manipulate expression directly inside the Manipulate expression itself, as well as in the initialization section where this is usually done.
Both methods work, but the semantics are not the same when it comes to direct access to manipulating dynamics from a module (against them they are passed as arguments to the module, which is actually the best method, but I'm trying to do something now)
I don’t know how these things are implemented, but I’m worried that if I put all the modules inside the Manipulate expression, then Manipulate will slow down when there are many modules there, since every time it needs to update the expression, Mathematica FE will send to the kernel now much larger expression for reassessment / parsing or any correct term.
Now the updated Manipulate expression is much larger, since the modules are now part of the Manipulate expression itself and are also located in the initialization section, and this happens, although some of them may not be called in every update.
To explain this question in more detail, I made a small diagram below to show what I mean, side by side, comparing the two methods. Below I also put small code examples used in diagrams:
for the Part-of-expression method
Manipulate[ foo[]:=Module[{}, x++ ]; ctrl; foo[], Button["step",{ctrl++ }], {{ctrl,0},None}, {{x,0},None}, TrackedSymbols:>{ctrl} ]
code for module in initialization
Manipulate[ ctrl; foo[], Button["step", {ctrl++ }], {{ctrl, 0}, None}, {{x, 0}, None}, TrackedSymbols :> {ctrl}, Initialization :> { foo[] := Module[{}, x++ ] } ]
The question is: will there be performance in expression methods inside the module?
note added:
Btw, in my current small demo, I did not notice differences in the performance of both methods, but this is only based on observing the demo and without accurate measurements. Perhaps my understanding of the Manipulate Initialization section was incorrect all the time. From the certificate it says:
Initialization is an option for Dynamic, DynamicModule, Manipulate, and related constructs that specifies an expression to be evaluated when the construct is first used or displayed.
And it looks like maybe I interpreted it as another way to say what it is.
Maybe Manipulate always evaluated all modules every time as part of an update or an expression update?
In any case, I would be happy if there were no difference in performance between the two layouts, since now I have placed all the modules inside the Manipulate expression itself, and not in the "Initialization" section.
addition, dec. 19, 2001 11:00 I looked at the decision of Mr. Master, posted below. From what I see when looking at the Manipulate snapshot, the resulting code is equivalent to explicitly placing the module inside the Manipulate expression. Here is a screenshot showing both methods and the resulting code (generate using the Manipulate Function button using the snapshot option button) for each layout. We see that this is the same code.
But the trick Mr Wizard used to enable a function in Control-> None, which should only write foo
instead of foo[]
, I would not have thought. I always thought it was necessary to write foo[]:=Module[...]
, even if foo
does not accept arguments. (in fact, I didn’t even think about it, of course, I write [] at the end of each function name, even if it does not accept any arguments). Thanks for sharing this trick.