home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / PROGRAMM.PAK / SHOWTIME.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  1.3 KB  |  58 lines

  1.  
  2. (*********************************************************************
  3.  
  4.         Adapted from
  5.         Roman E. Maeder: Programming in Mathematica,
  6.         Second Edition, Addison-Wesley, 1991.
  7.  
  8.  *********************************************************************)
  9.  
  10.  
  11. BeginPackage["Utilities`ShowTime`"]
  12.  
  13. ShowTime::usage = "On[ShowTime] turns timing info on. Off[ShowTime] turns it off again.
  14.     The time taken for each command is printed before the result (if any)."
  15.  
  16. ShowTime::twice = "ShowTime is already on."
  17. ShowTime::off = "ShowTime is not in effect."
  18.  
  19. Begin["`Private`"]
  20.  
  21. oldPre =.        (* user's value of $Pre *)
  22. ison = False     (* whether ShowTime is currently turned on *)
  23.  
  24. Attributes[ShowTime] = HoldFirst
  25.  
  26. ShowTime[expr_] :=
  27.     Module[{timing},
  28.         Block[{$Pre = oldPre},
  29.             timing = Timing[ oldPre[expr] ];
  30.             Print[ timing[[1]] ];
  31.             oldPre = If[ ValueQ[$Pre], $Pre, Identity ];
  32.         ];
  33.         If[ !ison, $Pre = oldPre ];  (* turn it off again *)
  34.         timing[[2]]
  35.     ]
  36.  
  37. ShowTime/: On[ShowTime] := (
  38.     If[ ison,
  39.         Message[ShowTime::twice],
  40.     (* else *)
  41.         oldPre = If[ ValueQ[$Pre], $Pre, Identity ];
  42.         $Pre = ShowTime;
  43.         ison = True
  44.     ]; )
  45.  
  46. ShowTime/: Off[ShowTime] := (
  47.     If[ ison,
  48.         ison = False,
  49.     (* else *)
  50.         Message[ShowTime::off]
  51.     ]; )
  52.  
  53. End[]
  54.  
  55. EndPackage[]
  56.  
  57. On[ShowTime]
  58.