What generates the In/Out CellLabels in Mathematica and how can I add automatic Timing to them?

♀尐吖头ヾ 提交于 2019-12-04 04:22:41

Another way of doing this would be to set EvaluationCompletionAction -> "ShowTiming" which will display timing information in the status bar of the notebook window after each evaluation.

OK, the discussion on Physics Forums has lead to this quite hackish solution (now cleaned up a little):

SetAttributes[Timeit, HoldAll]
Timeit[x_] := With[{t = Timing[x]}, Module[{out, form},
  If[TrueQ[MemberQ[$OutputForms, Head[t[[2]]]]],
    out = First[t[[2]]]; form = "//" <> ToString[Head[t[[2]]]], 
    out = t[[2]]; form = ""];
  If[out === Null, Null,
    CellPrint[ExpressionCell[t[[2]], "Output", CellLabelAutoDelete -> False,
      CellLabel -> StringJoin["(", ToString[t[[1]]], ")",
        "Out[", ToString[$Line], "]", form, "="]]];
  Unprotect[Out]; Out[$Line] = out; Protect[Out]; out;]];]
$Pre = Timeit;

To make the CellLabels persistent so that you don't lose the timing when you Save and Load the notebook, you can modify the stylesheet so that the Output cells have the option CellLabelAutoDelete -> True. (Edit: Now added to the CellPrint command.)

Any better solutions are more than welcome.

It's possible to add evaluation timestamps to Mathematica cell labels by selecting "AddTimeStamp" in the option inspector settings for EvaluationCompletionAction. Moreover, a list of options can be given by editing the entry, so using {"ShowTiming","AddTimeStamp"} I get both an evaluation duration in the status bar and input and output timestamps in the cell labels.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!