home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / TIMER / README < prev    next >
Text File  |  1993-12-01  |  5KB  |  109 lines

  1. October 1991
  2.  
  3. Fred C. Smith
  4. 617-438-5471 (home)
  5. uunet!samsung!wizvax!fcshome!fredex
  6.  
  7. I have taken the routines as described below and modified elapsedtime() so
  8. that it now returns an unsigned long representing the number of milliseconds
  9. elapsed since start and stop. The version described below by Mr. Pentcheff
  10. returns a double representing the elapsed time in seconds. This works just
  11. fine, but I didn't want to pay the price (in bloat) of having to load
  12. floating-point code into the program just to get this feature.
  13.  
  14. The function prototypes listed at the bottom have been updated to reflect
  15. this change.
  16.  
  17. NOTE that the existing documentation in this package can't make up it's
  18. mind whether this provides MILLIsecond or MICROsecond precision. Well,
  19. it's actually MILLIsecond.
  20.  
  21. This stuff, as it stands now, compiles and runs just fine when compiled
  22. with Microsoft QuickC 2.0. I assume that it will also work with later
  23. versions of QuickC and with MSC 6.0--but not MSC 5.1, as it lacks the
  24. in-line assembler support. NOTE that I have not updated the makefiles
  25. for Microsoft C. It's not a big deal to compile, though. for QC2.0
  26. just do:
  27.  
  28.     qcl -c timer.c
  29.  
  30. and you will get timer.obj, for inclusion in other programs.
  31.  
  32. Also, I have included a copy of timer.obj, compiled with QC 2.0, for
  33. those of you you don't have a suitable version of Microsoft C.
  34. ------------------------------------------------------------------------
  35.  
  36.  
  37. Dean Pentcheff
  38. Department of Integrative Biology
  39. University of California at Berkeley
  40. dean@violet.berkeley.edu
  41.  
  42. The code here provides PC programs with a millisecond resolution timer.  It
  43. is entirely based on "tctimer" 1.0 by Richard S. Sadowsky (released to the
  44. public domain 8/10/88) which, in turn, is based on "tptime" by Brian Foley
  45. and Kim Kokkonen of TurboPower Software (also public domain).  I have
  46. slightly rewritten the code to conform to C language conventions (in tctimer,
  47. results were passed as argument pointers rather than function returns).  In
  48. this version, elapsedtime() returns a time in seconds rather than
  49. milliseconds, saving a floating point division by 1000.0.
  50.  
  51. These routines reprogram the timer chip, so they probably won't work if your
  52. program does the same.  Other than that, there should be no incompatibility
  53. problems.  They do not interfere with Turbo's sound() and nosound()
  54. functions.
  55.  
  56. I've tested these under Turbo C 2.0.  Note that the file tctimer.c must be
  57. separately compiled with the standalone "tcc" compiler since it uses inline
  58. assembly.  Attempting to compile it under "tc" will result in compile-time
  59. error messages.
  60.  
  61. The routine "initializetimer()" MUST be called first to reset the timer.  The
  62. calling program MUST also call "restoretimer()" before exiting to reset the
  63. timer to its original state.
  64.  
  65.  
  66. The following routines are included (see testtimer.c for examples):
  67.  
  68. /*Reprogram the timer chip to allow 1 millisecond resolution*/
  69. void initializetimer(void);
  70.  
  71. /*Read the timer with 1 millisecond resolution*/
  72. long readtimer(void);
  73.  
  74. /*Calculate time elapsed (in milliseconds) between Start and Stop*/
  75. unsigned long    elapsedtime(long start, long stop);
  76.  
  77. /*Restore the timer chip to its normal state*/
  78. void restoretimer(void);
  79.  
  80.  
  81. Limitations
  82. -----------
  83. Because long integers are used to represent time, TCTIMER cannot be used to
  84. time events longer than about 60 minutes:
  85.  
  86.   4,294,967,295 (= $FFFFFFFF, largest unsigned value represented by longint)
  87. /     1,193,181 (timer resolution in counts/second)
  88. ---------------
  89.           3,599
  90.         /    60 (seconds/minute)
  91.         -------
  92.            59.9 minutes
  93.  
  94. This should hardly be a problem, however, since an event longer than an hour
  95. presumably doesn't need to be timed with 1-microsecond accuracy anyway.
  96.  
  97. Also note that the process of reading the time takes time. Hence, results of
  98. timing very short events will be skewed by the overhead of reading the timer.
  99. The following table shows the time measured between two calls to ReadTimer,
  100. one right after the other.
  101.  
  102.   Toshiba 1000 (4.77MHz 8088)    125 microseconds  (tctimer)
  103.   ATT 6300 (8MHz 8086)            53     "         (tctimer)
  104.   Deskpro 286 (8MHz 80286)        35     "         (tctimer)
  105.   Sperry IT (7.1MHz 286, 0 wait)  32     "         (tctimer)
  106.   IBM PS/2 model 50               25     "         (tctimer)
  107.   PC Designs GV386 (16MHz)        27     "         (tctimer)
  108.   PC Source Standard 286 (10MHz)  21     "         (these routines)
  109.