Message Boards Message Boards

Bad but Short Mathematica Programs

GROUPS:

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
Answer
9 days ago

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
Answer
9 days ago

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

POSTED BY: Updating Name
Answer
9 days ago

You are right, corrected.

POSTED BY: Kuba Podkalicki
Answer
8 days ago

Group Abstract Group Abstract