Skip to content

Instantly share code, notes, and snippets.

@lshifr
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lshifr/71977c7c0ea7469c5bcf to your computer and use it in GitHub Desktop.
Save lshifr/71977c7c0ea7469c5bcf to your computer and use it in GitHub Desktop.
Tracking cell execution time in Mathematica
BeginPackage["CellTimeTrack`"]
CellTimeTrack::usage = "CellTimeTrack[True|False] switches on or off the cell time-tracking mode";
Begin["`Private`"]
$style = Function[
arg,
StyleBox[arg, FontSize -> 16, FontWeight -> Bold, FontColor -> Darker[Green, 0.5]]
];
now := AbsoluteTime[];
rightLabel = Function[lab, Dynamic[{{None, lab}, {None, None}}], HoldAll];
refresh = Function[code, Refresh[code, UpdateInterval -> 1], HoldAll];
elapsed[start_] := ToString[Round[now - start]];
codegen[code_]:=code //. Join[OwnValues[refresh], OwnValues[$style], OwnValues[now], DownValues[elapsed]];
timeLabel[start_] := codegen @ rightLabel[refresh[$style@elapsed[start]]];
SetAttributes[set, HoldRest];
set[opts_List] := SetOptions[EvaluationCell[], opts];
set[label_, endCode_] := set[{CellFrameLabels -> label, CellEpilog :> endCode}];
$track := set[timeLabel[now], set[{{None, None}, {None, None}}, None]];
$oldPre = Null;
setPre[f_] := $Pre = Function[code, $track; f[code], HoldAll];
Unprotect[CellTimeTrack];
CellTimeTrack[True] :=
If[$oldPre === Null || ! ValueQ[$Pre] ,
setPre[ $oldPre = $Pre; If[ValueQ[$Pre], $Pre, Identity]];
];
CellTimeTrack[False] :=
If[ $oldPre =!= Null,
Unset[$Pre]; If[$Pre =!= $oldPre, $Pre = $oldPre]; $oldPre = Null
];
SetAttributes[CellTimeTrack, {Protected, ReadProtected}];
End[]
EndPackage[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment