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

后端 未结 3 984
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-16 00:07

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

相关标签:
3条回答
  • 2021-02-16 00:46

    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.

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

    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)
  • 2021-02-16 01:01

    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)
提交回复
热议问题