/* ---------- "It's ShowTime[], folks!" ---------- */
There has been a question about making Mathematica packages available
over the net. So I decided to give it a try with a little utility
that I concocted one day.

Mathematica has a Timing[] command that returns the time it took
for an evaluation together with the result. Sometimes it would
be nicer however if it would just print the time and return the
original result. Such a function is available in Reduce (on time;),
Macsyma (showtime: true;) and certainly in others as well.

Here are two versions for Mathematica. The first one is
the easiest solution I could think of using the variable $Pre.
If this variable has a value this value is applied to your
input before evaluation. Setting it to the name of a function
that evaluates the input, prints the time and then returns
the result is easy. Here it is:

=================== ShowTime0.m ===================
(* ShowTime0.m copyright 1989 by R. Maeder *)

(* permission is granted to copy this file provided
that the copyright notice is left intact and the
copies are not distributed commercially. *)

ShowTime::usage = "$Pre=ShowTime turns timing info on.
$Pre=. turns it off again.
The time taken for each command is printed just before
the results (if any) of the command."

Attributes[ShowTime] = HoldFirst

ShowTime[expr_] :=
Block[{res},
res = Timing[expr];
Print[ res[[1]] ];
res[[2]]
]

$Pre=ShowTime (* turn it on *)
=================== end of ShowTime0.m ===================

ShowTime[] must be HoldFirst otherwise evaluation would
already have occured before it got called.

Here is a sample run:

SunMathematica (sun3.68881) 1.1 (September 14, 1988) [With pre-loaded data]
by S. Wolfram, D. Grayson, R. Maeder, H. Cejtin,
S. Omohundro, D. Ballman and J. Keiper
Copyright 1988 Wolfram Research Inc.
-- SunView graphics initialized --

In[1]:= <<ShowTime0.m

Out[1]= ShowTime

In[2]:= Expand[(x+2)^10]
0.0166667 Second

2 3 4 5 6
Out[2]= 1024 + 5120 x + 11520 x + 15360 x + 13440 x + 8064 x + 3360 x +

7 8 9 10
> 960 x + 180 x + 20 x + x

In[3]:= Factor[%]
0.583333 Second

10
Out[3]= (2 + x)

This is fine if you don't need $Pre for something else.
The second version uses $Pre in a transparent way so you can
still use it. It uses scoping and explicit tests for changes
to the value of $Pre to achieve this. It is not completely
transparent. If you end a statement with a semicolon
then % cannot be used to refer to it.

Here it is in a shar archive, enjoy!

Roman E. Maeder maeder@symcom.math.uiuc.edu
Department of Mathematics and
Center for Supercomputing Research and Development
Univ. of Illinois at Urbana-Champaign

-----Cut Here-----Cut Here-----Cut Here-----Cut Here-----
#!/bin/sh
# shar: Shell Archiver
# Run the following text with /bin/sh to create:
# ShowTime.m
sed 's/^X//' << 'SHAR_EOF' > ShowTime.m
X(*% @(#)ShowTime.m 1.5 89/02/06, copyright 1988 by R. Maeder *)
X
X(* permission is granted to copy this file provided
X that the copyright notice is left intact and the
X copies are not distributed commercially. *)
X
XShowTime::usage = "On[ShowTime] turns timing info on.
X Off[ShowTime] turns it off again.
X The time taken for each command is printed just before
X the results (if any) of the command."
X
XBegin["ShowTime`"]
X
X`oldPre =.
X`offtime =.
X`res =.
X
XAttributes[ShowTime] = HoldFirst
X
XShowTime[expr_] := (
X Block[{$Pre = oldPre},
X res = Timing[oldPre[expr]];
X Print[res[[1]]];
X oldPre = If[ValueQ[$Pre], $Pre, Identity];
X ];
X res = res[[2]];
X If[SameQ[res, offtime], $Pre = oldPre; res = Null];
X res )
X
XShowTime/: On[ShowTime] := (
X If[ValueQ[$Pre],
X oldPre = $Pre,
X oldPre = Identity];
X $Pre = ShowTime;
X Null)
X
XShowTime/: Off[ShowTime] := offtime (* catch it outside block *)
X
XEnd[]
X
XOn[ShowTime]
SHAR_EOF
exit 0