3
$\begingroup$

I have a Mathematica notebook that has a number of cells, and each cell has a number of code lines. Sometimes it takes long hours to finish evaluating each cell and I want to compare their evaluation times. I would like to know how much each cell has taken (wall-clock time) in terms of evaluation and would like to have that as output printed after each cell evaluates, and before the kernel moves on to next cell.

Is there a simple way to do this? For example, something like a stopwatch ON and OFF commands that I can wrap around the code written inside each cell?

Note that the option to ShowTiming in Options Inspector is not helpful to me here, as it shows the timing for the last cell, but not each. When I run a Notebook and it takes long time, say 2 days, it would be nice to see how long each cell took on its own.

$\endgroup$
1
  • $\begingroup$ You may wrap Timing around each cell. $\endgroup$ Oct 10, 2021 at 9:26

2 Answers 2

4
$\begingroup$

In the OptionInspector, you can set EvaluationCompletionAction to "AddTimeStamp". This prepends the Date and Time to all In and Out cell labels. This is almost what you want, but requires some post-processing - subtracting the innie from the outie to get the elapsed time. This can probably be automated.

$\endgroup$
3
$\begingroup$

Here's a stylesheet based option:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[
            StyleData["Input"],
            CellProlog :> (in=AbsoluteTime[]),
            CellEpilog :> (
                CurrentValue[EvaluationCell[],CellFrameLabels] = 
                    {
                    {None, Cell @ BoxData @ StyleBox[
                        RowBox[{"(",ToBoxes[NumberForm[AbsoluteTime[]-in, {Infinity, 1}]], ")"}],
                        Plain]
                    },
                    {None,None}
                    }
                )
        ]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]

Example (as a screenshot):

enter image description here

Notice the time in parentheses on the right hand side.

$\endgroup$
1
  • $\begingroup$ This does not work on Cells with the style "Code". Can that be fixed? $\endgroup$ Oct 24, 2021 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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