Any performance issues with defining modules as part of a Manipulate vs. expression in the initialization section?

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:

enter image description here

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.

enter image description here

+7
source share
2 answers

I think this should be application specific. You can insert Pause[1]; expressions Pause[1]; before foo[]:= in both examples to confirm that revaluation is not performed in the Initialization method, and in the other in the other. The question of how long it takes to evaluate your expressions is that you are in a better position to answer. Why don't you put all your definitions in a big Do cycle and time?

In addition, although I would not recommend it usually, since you work under restrictions, perhaps you open such a hacker:

 Manipulate[ ctrl; foo[], Button["step", {ctrl++}], {{ctrl, 0}, None}, {{x, 0}, None}, {{foo, Module[{}, x++] &}, None}, TrackedSymbols :> {ctrl} ] 
+4
source

Responding to your addition, Dec. 19, 2001 11:00 :

As you can see in your own screen capture above, the code is actually not the same. In the upper example, the definition of foo is inside the DynamicModule variable DynamicModule , and not in the body where it will be re-evaluated. I believe that this is exactly what you need, right?

You wrote:

But the trick Mr. Wizard used to include the function in the control -> No, which only foo should write instead of foo [] is something I would not think about. 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.

Do not miss the mechanism by which this works. (By the way, I really hated writing Module[{} ... , but I copied it from your code. Please allow us to avoid this construct in the future.)

  • It is generally correct to use the form function[] rather than function when you want to perform an action. Although this is entirely possible, it contradicts the nature and syntax of Mathematica to trigger an action with only the function name. It also makes it difficult to process the function itself without initiating an evaluation.

  • You can get the behavior above using & without Slot arguments.

If we define doSomething = Print["Something Done!"] & Then we call it with doSomething[] to print the line. We can still write or pass a function using doSomething without running an evaluation.

+1
source

All Articles