Dec 242017
High resolution timers for TP V4.0+ by Kim Kokkenon. Full source.
File TPTIMER.ZIP from The Programmer’s Corner in
Category Pascal Source Code
High resolution timers for TP V4.0+ by Kim Kokkenon. Full source.
File Name File Size Zip Size Zip Type
BENCH.PAS 2023 908 deflated
TPTIMER.ASM 923 425 deflated
TPTIMER.DOC 3306 1408 deflated
TPTIMER.PAS 5868 1583 deflated
TTDEMO.PAS 496 265 deflated

Download File TPTIMER.ZIP Here

Contents of the TPTIMER.DOC file

TPTIMER - Routines for high-resolution timing of events
Brian Foley and Kim Kokkonen
TurboPower Software
Version 2.1
Released to the public domain

One problem commonly faced when trying to run benchmarks on a PC is that, by
default, the system clock is accurate only to 1/18th of a second. The TPTIMER
unit provides a simple and convenient means of timing events with microsecond
resolution. It does this by reprogramming the timer chip, but the gory details
are hidden from you. TPTIMER automatically reprograms the timer before your
program starts, then restores it to its normal state when your program ends.
Unless your program is working with the timer chip at a very low level, no
incompatibilities should arise, nor should the performance of your program

TPTIMER is very easy to use. You just add it to your program's USES statement
and call the ReadTimer function when you are ready to start/stop timing. For a
simple demonstration of how to use TPTIMER, see BENCH.PAS.

TPTIMER interfaces the following routines:

function ReadTimer : LongInt;
{-Read the timer with 1 microsecond resolution}

function ElapsedTime(Start, Stop : LongInt) : Real;
{-Calculate time elapsed (in milliseconds) between Start and Stop}

function ElapsedTimeString(Start, Stop : LongInt) : string;
{-Return time elapsed (in milliseconds) between Start and Stop as a string}

procedure InitializeTimer;
{-Reprogram the timer chip to allow 1 microsecond resolution}

procedure RestoreTimer;
{-Restore the timer chip to its normal state}

The first three of these are probably the only ones you'll ever need to use.
InitializeTimer is executed automatically before your program begins,
RestoreTimer when it ends. You shouldn't call these yourself unless you want
to reset the timer to its normal state temporarily, as you might before using
the Exec procedure in the DOS unit:


Because long integers are used to represent time, TPTIMER 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)
/ 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.
As of version 2.0, TPTIMER executes a calibration routine to try to compensate
for this overhead as much as possible. This routine estimates the amount of
time required to read the timer twice, and uses this value in ElapsedTime and
ElapsedTimeString to adjust for the overhead. Even so, you should expect an
error due to overhead of about 1-4 ms.

 December 24, 2017  Add comments

Leave a Reply