home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Boston 2
/
boston-2.iso
/
DOS
/
PROGRAM
/
C
/
TIMER
/
README
< prev
next >
Wrap
Text File
|
1993-12-01
|
5KB
|
109 lines
October 1991
Fred C. Smith
617-438-5471 (home)
uunet!samsung!wizvax!fcshome!fredex
I have taken the routines as described below and modified elapsedtime() so
that it now returns an unsigned long representing the number of milliseconds
elapsed since start and stop. The version described below by Mr. Pentcheff
returns a double representing the elapsed time in seconds. This works just
fine, but I didn't want to pay the price (in bloat) of having to load
floating-point code into the program just to get this feature.
The function prototypes listed at the bottom have been updated to reflect
this change.
NOTE that the existing documentation in this package can't make up it's
mind whether this provides MILLIsecond or MICROsecond precision. Well,
it's actually MILLIsecond.
This stuff, as it stands now, compiles and runs just fine when compiled
with Microsoft QuickC 2.0. I assume that it will also work with later
versions of QuickC and with MSC 6.0--but not MSC 5.1, as it lacks the
in-line assembler support. NOTE that I have not updated the makefiles
for Microsoft C. It's not a big deal to compile, though. for QC2.0
just do:
qcl -c timer.c
and you will get timer.obj, for inclusion in other programs.
Also, I have included a copy of timer.obj, compiled with QC 2.0, for
those of you you don't have a suitable version of Microsoft C.
------------------------------------------------------------------------
Dean Pentcheff
Department of Integrative Biology
University of California at Berkeley
dean@violet.berkeley.edu
The code here provides PC programs with a millisecond resolution timer. It
is entirely based on "tctimer" 1.0 by Richard S. Sadowsky (released to the
public domain 8/10/88) which, in turn, is based on "tptime" by Brian Foley
and Kim Kokkonen of TurboPower Software (also public domain). I have
slightly rewritten the code to conform to C language conventions (in tctimer,
results were passed as argument pointers rather than function returns). In
this version, elapsedtime() returns a time in seconds rather than
milliseconds, saving a floating point division by 1000.0.
These routines reprogram the timer chip, so they probably won't work if your
program does the same. Other than that, there should be no incompatibility
problems. They do not interfere with Turbo's sound() and nosound()
functions.
I've tested these under Turbo C 2.0. Note that the file tctimer.c must be
separately compiled with the standalone "tcc" compiler since it uses inline
assembly. Attempting to compile it under "tc" will result in compile-time
error messages.
The routine "initializetimer()" MUST be called first to reset the timer. The
calling program MUST also call "restoretimer()" before exiting to reset the
timer to its original state.
The following routines are included (see testtimer.c for examples):
/*Reprogram the timer chip to allow 1 millisecond resolution*/
void initializetimer(void);
/*Read the timer with 1 millisecond resolution*/
long readtimer(void);
/*Calculate time elapsed (in milliseconds) between Start and Stop*/
unsigned long elapsedtime(long start, long stop);
/*Restore the timer chip to its normal state*/
void restoretimer(void);
Limitations
-----------
Because long integers are used to represent time, TCTIMER cannot be used to
time events longer than about 60 minutes:
4,294,967,295 (= $FFFFFFFF, largest unsigned value represented by longint)
/ 1,193,181 (timer resolution in counts/second)
---------------
3,599
/ 60 (seconds/minute)
-------
59.9 minutes
This should hardly be a problem, however, since an event longer than an hour
presumably doesn't need to be timed with 1-microsecond accuracy anyway.
Also note that the process of reading the time takes time. Hence, results of
timing very short events will be skewed by the overhead of reading the timer.
The following table shows the time measured between two calls to ReadTimer,
one right after the other.
Toshiba 1000 (4.77MHz 8088) 125 microseconds (tctimer)
ATT 6300 (8MHz 8086) 53 " (tctimer)
Deskpro 286 (8MHz 80286) 35 " (tctimer)
Sperry IT (7.1MHz 286, 0 wait) 32 " (tctimer)
IBM PS/2 model 50 25 " (tctimer)
PC Designs GV386 (16MHz) 27 " (tctimer)
PC Source Standard 286 (10MHz) 21 " (these routines)