Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In Wolfram's tutorial on Advanced Manipulate Functionality, a nice example of a continually evaluating Manipulate cell is presented:

Manipulate[
  f[x_] := x^3;
  Graphics[{Thickness[0.01], Line[{{0, 0}, {n, f[n]}}]}, 
    PlotRange -> 1],
  {n, -1, 1}]

If I wrap Timing[] around this example I get list of 0. along with the continually evaluating Manipulate cell.

Now can anybody make a clock that will show increasing time of evaluation of such a Manipulate cell? In case the Manipulate cell has finite execution time, the clock would show time elapsed for this execution. The motivation behind is to have measure of slow Manipulate cell execution.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Plot on its own does not take much CPU, added some dummy computation and count of how many refreshes has happened to make it more interesting. Is this what you meant?

You do not wrap Timing around the whole of Manipulate. To measure the CPU taken for each Manipulate refresh of its expression, which happens each time a control dynamic changes, just make a running counter and a running cpu time count and update as needed. You can use these to find the average time a refresh takes to complete.

Manipulate[
 Module[{t, g},

  {t, g} = Timing[PrimeQ[#] & /@ RandomInteger[10000, 100000];
    Graphics[{Thickness[0.01], Line[{{0, 0}, {n, f[n]}}]}, PlotRange -> 1]];

  runningTime += t;
  count++;

  Grid[{{"count", "current refresh CPU", "total CPU so far", "mean CPU per refresh"},
        {count, t, runningTime, runningTime/count},
        {g, SpanFromLeft}},
        Frame -> All, Spacings -> {1, 2}]],

 {n, -1, 1},
 {{runningTime, 0}, None},
 {{meanCPU, 0}, None},
 {{count, 0}, None},

 TrackedSymbols :> {n},

 Initialization ->
  (
   f[x_] := x^3;
   )
 ]

Mathematica graphics

share|improve this answer
add comment

A fairly simple way to add a clock to your Manipulate is to use SessionTime.

Manipulate[
  f[x_] := x^3;
  Column[{
    Graphics[{Thickness[0.01], Line[{{0, 0}, {n, f[n]}}]}, PlotRange -> 1],
    Row[{"Elapsed time (sec): ", SessionTime[] - start}]},
    Frame -> All],
  {n, -1, 1},
  {{start, SessionTime[]}, None}]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.