# How can I track the execution time of an evaluating Manipulate cell?

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.

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;
)
]


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}]