home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / CONTRIB / PHLIPP.SA < prev    next >
Text File  |  1994-11-15  |  3KB  |  95 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- timer.sa: For timing short things.  NON-PORTABLE.  This really
  9. -- should be portably integrated into UNIX or SYS.  Any volunteers?
  10. --
  11. -- Author: Michael Philippsen <phlipp@icsi.berkeley.edu>
  12. -- Copyright (C) 1994, International Computer Science Institute
  13. ------------------------------------------------------------------------
  14.  
  15. class TIMER is
  16.  
  17.   -- Has a routine for starting a Timer and a Routine that returns
  18.   -- the Time Elapsed since the start.
  19.   -- Usage may be as follows:
  20.   --       
  21.   --     TIMER::Start;
  22.   --     
  23.   --            Section of your Porgram that should be timed
  24.   --            
  25.   --     #OUT+"Elapsed Time: "+TIMER::Elapsed+" seconds \n";
  26.   
  27.   ----------------------------------------------------------------------
  28.  
  29.   private const CLOCKS_PER_SEC:INT:= 1000000;  -- clock returns milliseconds
  30.                                                -- see "man 3 clock" for details
  31.                                                -- resolution of clock is
  32.                                                -- 16.667 milliseconds
  33.   
  34.   private const DEBUG:BOOL:=false;
  35.   
  36.   private shared startOfTimer:INT;  
  37.  
  38.   ------------------------------------------------------------
  39.     
  40.   Start is
  41.     if (void(startOfTimer)) then
  42.       startOfTimer := C_CLOCK::clock;
  43.     end;
  44.     startOfTimer := C_CLOCK::clock;
  45.   end; --Start
  46.   
  47.   ------------------------------------------------------------
  48.   
  49.   Elapsed:FLT is
  50.     if void(startOfTimer) then
  51.       return startOfTimer.flt;
  52.     else
  53.       endOfTimer:INT:=C_CLOCK::clock;
  54.       if DEBUG then
  55.         #OUT+"Elapsed non void start: "+startOfTimer+"\n";
  56.         #OUT+"                 end:   "+endOfTimer+"\n";
  57.       end;
  58.       res:FLT:=((endOfTimer - startOfTimer).flt / CLOCKS_PER_SEC.flt);
  59.       if DEBUG then
  60.         #OUT+"Seconds: "+res+"\n";
  61.       end;
  62.       return res;
  63.     end;
  64.   end; --Elapsed
  65.     
  66. end; -- TIMER
  67.  
  68. ----------------------------------------------------------------------
  69.  
  70. external class C_CLOCK is
  71.   clock:INT;
  72. end;
  73.  
  74. ----------------------------------------------------------------------
  75.  
  76. class TEST_TIMER is
  77.  
  78.   include TEST;
  79.   
  80.   main is
  81.     class_name("TIMER");
  82.  
  83.     test("illegal Elapsed",TIMER::Elapsed.str,"0");
  84.     
  85.     TIMER::Start;
  86.     loop 1000000.times!; end;
  87.     
  88.     unchecked_test("legal Elapsed",TIMER::Elapsed.str,"~0.9");
  89.     
  90.     finish;
  91.   end; --main
  92.   
  93. end; --TEST_TIMER
  94.  
  95.