home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / UTILITIE.PAK / SHOWTIME.M < prev   
Encoding:
Text File  |  1992-07-29  |  1.3 KB  |  59 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 information on. Off[ShowTime]
  14. turns it off again.  The time taken for each command is printed
  15. before the result (if any)."
  16.  
  17. ShowTime::twice = "ShowTime is already on."
  18. ShowTime::off = "ShowTime is not in effect."
  19.  
  20. Begin["`Private`"]
  21.  
  22. oldPre =.        (* user's value of $Pre *)
  23. ison = False     (* whether ShowTime is currently turned on *)
  24.  
  25. Attributes[ShowTime] = HoldFirst
  26.  
  27. ShowTime[expr_] :=
  28.     Module[{timing},
  29.         Block[{$Pre = oldPre},
  30.             timing = Timing[ oldPre[expr] ];
  31.             Print[ timing[[1]] ];
  32.             oldPre = If[ ValueQ[$Pre], $Pre, Identity ];
  33.         ];
  34.         If[ !ison, $Pre = oldPre ];  (* turn it off again *)
  35.         timing[[2]]
  36.     ]
  37.  
  38. ShowTime/: On[ShowTime] := (
  39.     If[ ison,
  40.         Message[ShowTime::twice],
  41.     (* else *)
  42.         oldPre = If[ ValueQ[$Pre], $Pre, Identity ];
  43.         $Pre = ShowTime;
  44.         ison = True
  45.     ]; )
  46.  
  47. ShowTime/: Off[ShowTime] := (
  48.     If[ ison,
  49.         ison = False,
  50.     (* else *)
  51.         Message[ShowTime::off]
  52.     ]; )
  53.  
  54. End[]
  55.  
  56. EndPackage[]
  57.  
  58. On[ShowTime]
  59.