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

后端 未结 2 2169
名媛妹妹
名媛妹妹 2021-02-16 00:23

When Mathematica evaluates a cell, it gives the Input cell and Output cell the CellLabels In[$Line]:= and Out[$Line]= where $Line is a counter that get

相关标签:
2条回答
  • 2021-02-16 00:43

    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.

    alt text

    0 讨论(0)
  • 2021-02-16 00:58

    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.

    0 讨论(0)
提交回复
热议问题