(*********************************************************************

Copyright 1989 by Roman E. Maeder

Adapted from
Roman E. Maeder: Programming in Mathematica, Addison-Wesley, 1989.

Permission is hereby granted to make copies of this file for
any purpose other than direct profit, or as part of a
commercial product, provided this copyright notice is left
intact. Sale, other than for the cost of media, is prohibited.

Permission is hereby granted to reproduce part or all of
this file, provided that the source is acknowledged.

*********************************************************************)

ShowTime::usage = "On[ShowTime] turns timing info on. Off[ShowTime] turns it off again.
The time taken for each command is printed before the result (if any)."

Begin["ShowTime`"]

`oldPre =. (* user's value of $Pre *)
`ison = False (* whether ShowTime is currently turned on *)

Attributes[ShowTime] = HoldFirst

ShowTime::twice = "ShowTime is already on."
ShowTime::off = "ShowTime is not in effect."

ShowTime[expr_] :=
Block[{`timing},
Block[{$Pre = oldPre},
timing = Timing[ oldPre[expr] ];
Print[ timing[[1]] ];
oldPre = If[ ValueQ[$Pre], $Pre, Identity ];
];
If[ !ison, $Pre = oldPre ]; (* turn it off again *)
timing[[2]]
]

ShowTime/: On[ShowTime] := (
If[ ison,
Message[ShowTime::twice],
(* else *)
oldPre = If[ ValueQ[$Pre], $Pre, Identity ];
$Pre = ShowTime;
ison = True
]; )

ShowTime/: Off[ShowTime] := (
If[ ison,
ison = False,
(* else *)
Message[ShowTime::off]
]; )

End[]

On[ShowTime]