Message Boards Message Boards

Bad but Short Mathematica Programs

Posted 6 years ago

Here are two programs. One of them is worse than the other. Which one is it?

Manipulate[FactorInteger[a], {a, 1, 100000000, 1}]

Manipulate[factored[[a]], {a, 1, 100000000, 1}, Initialization :> (factored = Table[FactorInteger[a], {a, 1, 100000000}]),SynchronousInitialization -> False]  

Hypothetically, the second program will perform slightly faster than the first. Realistically, the second program takes a long time to initialize. The first is rather zippy anyways. We can thus say that the second program is worse than the first. The lesson here is to not make a lot of unnecessary calculations.

What are some other examples of short, bad programs, and their superior replacements?

POSTED BY: Ed Pegg
3 Replies

Not using "nested Dynamics":

Manipulate[
  ContourPlot[Sin[5 x] Cos[7 y], {x, y} \[Element] Rectangle[],  
    Epilog -> {AbsolutePointSize@12, Point[t {1, 1}]}
  ],
  {t, 0, 1}
]

vs

Manipulate[
  ContourPlot[Sin[5 x] Cos[7 y], {x, y} \[Element] Rectangle[], 
    Epilog -> {AbsolutePointSize@12, Point[Dynamic[t] {1, 1}]}
  ],
  {t, 0, 1}
]

The second one is much smoother because the FrontEnd does not need to care about anything but t, when for the first case the whole ContourPlot is recalculated.

POSTED BY: Kuba Podkalicki
Posted 6 years ago

Both examples look the same to me. What am I missing?

POSTED BY: Updating Name

You are right, corrected.

POSTED BY: Kuba Podkalicki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract