home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / timer.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  1.8 KB  |  67 lines  |  [TEXT/R*ch]

  1. (* test/timer.sml
  2.    PS 1995-03-20
  3. *)
  4.  
  5. use "auxil.sml";
  6.  
  7. local 
  8.     fun fib n = if n<2 then 1 else fib(n-1) + fib(n-2);
  9.  
  10.     open Time Timer
  11.     val totalRealTime = startRealTimer ()
  12.     val totalCPUTime  = startCPUTimer ()
  13.  
  14. in
  15.  
  16. val test1 = check(checkRealTimer totalRealTime <= checkRealTimer totalRealTime
  17.           andalso (checkRealTimer totalRealTime before fib 25 seq ())
  18.                      < checkRealTimer totalRealTime);
  19.  
  20. local
  21.     val rtmr = startRealTimer ();
  22. in
  23. val test2 = check(checkRealTimer rtmr <= checkRealTimer rtmr
  24.           andalso (checkRealTimer rtmr before fib 25 seq ())
  25.                      < checkRealTimer rtmr);
  26. end
  27.  
  28. local
  29.     val op <= = fn ({usr=usr1, sys=sys1, gc=gc1}, {usr=usr2, sys=sys2, gc=gc2})
  30.     => usr1 <= usr2 andalso sys1 <= sys2 andalso gc1 <= gc1;
  31.     fun cput1 < cput2 = (cput1 <= cput2) andalso (cput1 <> cput2);
  32. in
  33. val test3 = check(checkCPUTimer totalCPUTime <= checkCPUTimer totalCPUTime
  34.           andalso (checkCPUTimer totalCPUTime before fib 25 seq ())
  35.                      < checkCPUTimer totalCPUTime);
  36. val ctmr = startCPUTimer ();
  37. val test4 = check(checkCPUTimer ctmr <= checkCPUTimer ctmr
  38.           andalso (checkCPUTimer ctmr before fib 25 seq ())
  39.                      < checkCPUTimer ctmr);
  40. end;
  41.  
  42. val _ = 
  43. let
  44.     fun time f arg =
  45.     let open Timer
  46.         val cputimer  = startCPUTimer ()
  47.         val realtimer = startRealTimer ()
  48.         val res = f arg
  49.         val {usr, sys, gc} = checkCPUTimer cputimer;
  50.         val rea = checkRealTimer realtimer;
  51.         fun format t = Time.toString t
  52.     in 
  53.         print("User: " ^ format usr ^
  54.         "  System: " ^ format sys ^ 
  55.         "  Gc: " ^ format gc ^ 
  56.         "  Real: " ^ format rea ^ "\n");
  57.         res
  58.     end;
  59.  
  60.     val _ = print "\nEach line below should show roughly \
  61.                    \the same User, System, and Gc times:\n";
  62. in
  63.     map (time fib) [28, 28, 28, 28, 28, 28, 28, 28] seq () 
  64. end 
  65.  
  66. end;
  67.