Mathematica - Show timing in output line

In summary, the "Timeit" function can be used to automatically display the time it takes to process a step in the output of a Mathematica notebook. This can be achieved by setting the variable $Pre to "Timeit". The function can also be modified to suppress the result and only show the timing, or to modify the output cell labels. However, these modifications may be lost when the notebook is saved and loaded, so it is recommended to set the CellLabelAutoDelete option to False or use the function as a notebook option instead.
  • #1
Hepth
Gold Member
464
40
While I know there exists:

In[1]:= Timing[Integrate[blah]]
Out[1]:= {result,time it took to calculate}

Is there a way to just have the time it took to process a step appear in the Out[1]:= part, or after every step rather than having to use the Timing function and then pull out the index?

If I say Result = Timing[process][[1]] then I get the result but don't see the timing, I'd have to put the Timing[xxx][[2]] after everything, and this is tedious. Can I customize the output lines to include this?
 
Physics news on Phys.org
  • #2
The Timing function actually returns the result in the other order
In[1]:= Timing[Integrate[blah]]
Out[1]:= {time, result}
where time is the process time of the kernel.

If you want to just suppress the result then you could use
In[1]:= Timing[Integrate[blah];]
Out[1]:= {time, Null}

If you don't want to have to type Timing every time, then there's a couple of options.

1) You could set the variable $Pre to
$Pre = Timing
or
$Pre = Timing[#][[1]] & (* just print timing, never the result *)

2) Go to the Option Inspector (Shift-Ctrl-O), Notebook Options > Evaluation Options > EvaluationCompletionAction and choose either ShowTiming and/or AddTimeStamp.
ShowTiming only shows the timing in the StatusArea -- it's not permanent.

3) What you really wanted was to change the CellLabels for Output cells. I'm not sure how to do this. I think that this is neither a Notebook option nor a StyleSheet option. It must be in another configuration location. (nb you can turn off the In/Out labels in the Preferences). Maybe using $Pre and $Post you could hack a way of modifying the Output CellLabel as it's generated. Look at the example in the CellLabel documentation.

I hope something there helps!
 
  • #3
Thanks! Screwing around with it I did:

Clear[$Pre];
Clear[$Post];
Clear[$Pre];
Clear[$Post];
Evaluation::Timing = "Process took `1` seconds to complete.";
$Pre = Timing;
$Post = (Message[Evaluation::Timing, #[[1]]]; #[[2]]) &;

(The first things are just from debugging and having to clear stuff up)

This makes a message after everything you output with the time it took to run. Now I just add this to the initialization stuff I have in every notebook and I'm good to go!

Thanks for the ideas.
 
  • #4
actually, while that works it gets real ugly later. I just made instead:

Evaluation::Timing = "Process took `1` seconds to complete.";
Timeit[x_] := (Message[Evaluation::Timing, Timing[x][[1]]]; Timing[x][[2]])

To use instead of Timing, this puts the time it took into a message while still giving me my output, though I wonder if it is evaluating twice...
 
  • #5
I don't think that your Timeit is working.
The inside expression gets evaluated before Timeit is called - so it always say that the time taken is 0.

Evaluation::Timing = "Process took `1` seconds to complete.";
Timeit[x_] := With[{t = Timing[x]}, (Message[Evaluation::Timing, t[[1]]]; t[[2]])]
SetAttributes[Timeit, HoldAll]
$Pre = Timeit;

This seems to work like a charm. Although I'd still like to have a way of using the CellLabel option rather than using a Message.
 
  • #6
I think I got it, but still working out some formatting stuff:

Code:
Timeit[x_] :=  With[{t = Timing[x]},   CellPrint[   ExpressionCell[StandardForm[t[[2]]], "Output", CellLabel -> StringJoin["(", ToString[t[[1]]], ")", "Out[", ToString[$Line], "]:="]]]]
SetAttributes[Timeit, HoldAll]
$Pre = Timeit;

Then if you run :

Integrate[Sin[x] Exp[-x/5] x^2/(1 - 5 x), {x, 43, 434652.532}]

does it work?
 
  • #7
but if you call the previous result with "%" it's not the output but the full thing... hmm.

\!\(\*
TagBox[
FormBox[
RowBox[{"Cell", "[",
RowBox[{
RowBox[{"BoxData", "[",
RowBox[{"TagBox", "[",
RowBox[{
RowBox[{"FormBox", "[",
RowBox[{
RowBox[{"RowBox", "[",
RowBox[{"{",
RowBox[{
RowBox[{"RowBox", "[",
RowBox[{"{",
RowBox[{"\<\"-\"\>", ",", "\<\"0.0006293585747159831`\"\>"}], "}"}],
"]"}], ",", "\<\"+\"\>", ",",
RowBox[{"RowBox", "[",
RowBox[{"{",
RowBox[{"\<\"0.`\"\>", ",", "\<\" \"\>",
",", "\<\"\[ImaginaryI]\"\>"}], "}"}], "]"}]}],
"}"}], "]"}], ",", "StandardForm"}], "]"}], ",",
"StandardForm", ",",
RowBox[{"Editable", "->", "True"}]}], "]"}], "]"}],
",", "\<\"Output\"\>", ",",
RowBox[{"CellLabel", "->", "\<\"(7.02)Out[4]:=\"\>"}]}], "]"}],
StandardForm],
StandardForm,
Editable->True]\)
 
  • #8
Nice work!
OK... this is getting a bit hackish.
But if you use the following then % (Out) works ok and you don't get Null's being printed when there is not meant to be an output.

Code:
Unprotect[Out];
SetAttributes[Timeit, HoldAll]
Timeit[x_] := With[{t = Timing[x]}, If[t[[2]] === Null, Null,
   CellPrint[
    ExpressionCell[t[[2]], "Output", 
     CellLabel -> 
      StringJoin["(", ToString[t[[1]]], ")", "Out[", ToString[$Line], 
       "]:="]]];
   Out[$Line] = t[[2]];]]
$Pre = Timeit;

One more thing... these CellLabels and thus Timings are lost when you Save then Load the notebook.
You can fix this by modifying the StyleSheet (copy the Output style from Core.nb into the notebook's stylesheet) so that the Output cells now have
Code:
CellLabelAutoDelete->False
Or by setting it as a notebook option
Code:
SetOptions[EvaluationNotebook[], CellLabelAutoDelete -> False]
which is easier, but also affects Input cells - so you don't lose the numbering when you modify them.
 
Last edited:
  • #10
Just for the sake of completeness, here's an updated version that plays nicer with the various $OutputForms:

Code:
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",
      CellLabel -> StringJoin["(", ToString[t[[1]]], ")",
        "Out[", ToString[$Line], "]", form, "="]]];
  Unprotect[Out]; Out[$Line] = out; Protect[Out]; out;]];]
$Pre = Timeit;
 

1. How can I show the timing of my code's execution in the output line in Mathematica?

To show the timing in the output line in Mathematica, you can use the Timing function. Simply wrap the code you want to time with Timing[...] and the output will include the time in seconds for the code to run.

2. Can I customize the format of the timing displayed in the output line?

Yes, you can customize the format of the timing displayed in the output line by using the TimingFormat option. This allows you to specify the units you want to display, such as seconds or milliseconds, as well as the overall format of the timing output.

3. Is it possible to display the timing for only a specific portion of my code?

Yes, you can use the TimeConstrained function to time a specific portion of your code. This function takes two arguments: the code you want to time and the time limit in seconds. The output will include the timing for only the portion of code that ran within the time limit.

4. Can I use the timing information to compare the efficiency of different versions of my code?

Yes, you can use the timing information in the output line to compare the efficiency of different versions of your code. By running each version with the Timing function, you can see which version runs faster and make any necessary optimizations.

5. Does the timing in the output line include the time it takes to load the Mathematica package?

No, the timing in the output line only includes the time it takes for your code to run. It does not include the time it takes to load any external packages or libraries used in your code. If you want to include this time, you can use the AbsoluteTiming function instead of Timing.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
14K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Differential Equations
Replies
5
Views
2K
Back
Top