[comp.lang.pascal] Timing in Turbo Pascal

tswingle@oucsace.cs.ohiou.edu (Tom Swingle) (10/28/90)

Does anybody know of a way to *accurately* time an event down to at least 1/100
of a second in Turbo Pascal?  I have tried using the GetTime procedure, but it
relies on DOS, which is only accurate to about 5/100 of a second. Turbo Pascal
has the Delay procedure which can accurately time to 1/1000 of a second.  Does
anybody know how they do this?

Here is what I would like to do.  I would like to create a little unit called
Timer which would have procedures for timing how long a block takes to execute
regardless of the speed of the machine it is working on.  It would work
something like this:

uses Timer;

var t:word;

begin
 MarkTime;  { Start counting now }

   . . . . . . .

 t:=TimeElapsed; { Check how much time elapsed in 1/100 seconds }
end.

This way, you could make a block take the same amount of time to execute on any
machine, regardless of that machine's speed by just looping until TimeElapsed
had reached the time you wanted it to.  But I found that it has to be accurate
to at least 1/100 of a second to be useful.  If anyone can help me it would be
greatly appreciated.  (Either E-mail or a follow-up post is fine)

nkraft@crash.cts.com (Norman Kraft) (10/28/90)

In article <2280@oucsace.cs.OHIOU.EDU> tswingle@oucsace.cs.ohiou.edu (Tom Swingle) writes:
>Does anybody know of a way to *accurately* time an event down to at least 1/100
>of a second in Turbo Pascal?  I have tried using the GetTime procedure, but it
>relies on DOS, which is only accurate to about 5/100 of a second. Turbo Pascal
>has the Delay procedure which can accurately time to 1/1000 of a second.  Does
>anybody know how they do this?

Well, what they do, on an AT machine at least, is call a BIOS function which
waits for a millisecond. Unfortunately, we have found that the accuracy of
this BIOS routine varies from machine to machine, and if one desires to have
timings with resolutions of 1ms, the Turbo delay funtion just won't do it.

>Here is what I would like to do.  I would like to create a little unit called
>Timer which would have procedures for timing how long a block takes to execute
>regardless of the speed of the machine it is working on.  It would work
>something like this:

Before you start this project, look at Abrash's "Zen of Assembly Language".
In the first several chapters he developes an assembler routine which does
not depend on the BIOS, but uses timer chips in a very efficient and 
accurate way. The "ZenTimer", as it is called, achieves a resolution
of 1 microsecond (1 millionth of a second), and uses the 8253. Abrash
uses it to time 4 cycle assembler routines: a serious challenge, that!


--------------------------------------------------------------------------
Norman R. Kraft	                           "Things should be as simple
Director, Software Development              as possible, but not simpler."
Postal Buddy Corporation, San Diego, CA     - Albert Einstein
INET nkraft@crash.cts.com                    
UUCP {hplabs!hp-sdd ucsd nosc}!crash!nkraft   Usual disclaimer applies...    
--------------------------------------------------------------------------
                                     

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (10/28/90)

In article <5307@crash.cts.com> nkraft@crash.cts.com (Norman Kraft) writes:
>In article <2280@oucsace.cs.OHIOU.EDU> tswingle@oucsace.cs.ohiou.edu (Tom Swingle) writes:
>>Does anybody know of a way to *accurately* time an event down to at least 1/100
>>of a second in Turbo Pascal?  I have tried using the GetTime procedure, but it
>>relies on DOS, which is only accurate to about 5/100 of a second. Turbo Pascal
>>has the Delay procedure which can accurately time to 1/1000 of a second.  Does
>>anybody know how they do this?
>
>Well, what they do, on an AT machine at least, is call a BIOS function which
>waits for a millisecond. 

Which version are you using?  On mine (5.5), all delay() does is run a 
do-nothing loop.  The loop gets calibrated during the startup.  Maybe that's
where you saw the BIOS call?  

Duncan Murdoch
dmurdoch@watstat.waterloo.edu

nkraft@crash.cts.com (Norman Kraft) (10/30/90)

In article <1990Oct28.142118.6756@maytag.waterloo.edu> dmurdoch@watstat.waterloo.edu (Duncan Murdoch) writes:
>>
>>Well, what they do, on an AT machine at least, is call a BIOS function which
>>waits for a millisecond. 
>
>Which version are you using?  On mine (5.5), all delay() does is run a 
>do-nothing loop.  The loop gets calibrated during the startup.  Maybe that's
>where you saw the BIOS call?  
>

On an 8088/8086 machine, the do nothing loop turns up, but on AT's there
is a BIOS function (used frequently by the Turbo Power folks...) that will
wait for one millisecond. Unfortunately, as previously stated, this call
does not result in very accurate timings. A do nothing loop is not very 
useful for timings, regardless of a variety of calibration algorithms,
and this shows up clearly with the TP 5.5 8086 timings. We had problem
after problem with this until we wrote our own timers.

--------------------------------------------------------------------------
Norman R. Kraft	                           "Things should be as simple
Director, Software Development              as possible, but not simpler."
Postal Buddy Corporation, San Diego, CA     - Albert Einstein
INET nkraft@crash.cts.com                    
UUCP {hplabs!hp-sdd ucsd nosc}!crash!nkraft   Usual disclaimer applies...    
--------------------------------------------------------------------------
                                     

north@manta.NOSC.MIL (Mark H. North) (10/31/90)

In article <5307@crash.cts.com> nkraft@crash.cts.com (Norman Kraft) writes:
>
>Before you start this project, look at Abrash's "Zen of Assembly Language".
>In the first several chapters he developes an assembler routine which does
>not depend on the BIOS, but uses timer chips in a very efficient and 
>accurate way. The "ZenTimer", as it is called, achieves a resolution
>of 1 microsecond (1 millionth of a second), and uses the 8253. Abrash
>uses it to time 4 cycle assembler routines: a serious challenge, that!
>
I find this hard to believe (microsecond accuracy). There are hardware
interrupts going on at unpredictable times so unless you take over the
whole machine (memory refresh and all...) how can you be assured of accuracy
in your timer function?

Mark



F
I
L
L
E
R

webb@uhccux.uhcc.Hawaii.Edu (Thomas Webb) (11/01/90)

In article <1308@manta.NOSC.MIL> north@manta.nosc.mil.UUCP (Mark H. North) writes:
>In article <5307@crash.cts.com> nkraft@crash.cts.com (Norman Kraft) writes:
>>
>>Before you start this project, look at Abrash's "Zen of Assembly Language".
>>In the first several chapters he developes an assembler routine which does
>>not depend on the BIOS, but uses timer chips in a very efficient and 
>>accurate way. The "ZenTimer", as it is called, achieves a resolution
>>of 1 microsecond (1 millionth of a second), and uses the 8253. Abrash
>>uses it to time 4 cycle assembler routines: a serious challenge, that!
>>
>I find this hard to believe (microsecond accuracy). There are hardware
>interrupts going on at unpredictable times so unless you take over the
>whole machine (memory refresh and all...) how can you be assured of accuracy
>in your timer function?
>
>Mark
>

Quite right.  Abrash actually develops two timers in his book.  The
higer accuracy timer rerquires you to turn off all interupts.  Its
primary purpose is to time small sections of assembly code.  For
timing full programs Abrash has a long term timer of more questionable
accuracy.  However, it is still a book that anyone with more than a
passing interest in assembly should read.  It is the last in a series
of books editied by Jeff Dunteman (sp?).  If you are considering
learning assembly you might look at this series.  

good luck,

-tom
.
.
.


-- 
===============================================================================
webb@uhccux.uhcc.Hawaii.edu  "The first duty in life is to assume a pose.
                              What the second is, no one has yet
					discovered."            -Oscar Wilde

eli@diogenes.gang.umass.edu (Eli Brandt) (11/01/90)

In article <1308@manta.NOSC.MIL> north@manta.nosc.mil.UUCP (Mark H. North) writes:
>In article <5307@crash.cts.com> nkraft@crash.cts.com (Norman Kraft) writes:
>>
>>Before you start this project, look at Abrash's "Zen of Assembly Language".
>>In the first several chapters he developes an assembler routine which does
>>not depend on the BIOS, but uses timer chips in a very efficient and 
>>accurate way. The "ZenTimer", as it is called, achieves a resolution
>>of 1 microsecond (1 millionth of a second), and uses the 8253. Abrash
>>uses it to time 4 cycle assembler routines: a serious challenge, that!
>>
>I find this hard to believe (microsecond accuracy). There are hardware
>interrupts going on at unpredictable times so unless you take over the
>whole machine (memory refresh and all...) how can you be assured of accuracy
>in your timer function?
>
>Mark
>
>
>
>F
>I
>L
>L
>E
>R

You may note that the original poster said uSec *resolution*, not accuracy.
DRAM refresh and such will obviously perturb the results.  Abrash 
recommends averaging several runs.  If you are at all interested in '86
assembly, check out his book.  You'll never again believe a compiler 
vendor's claim that the compiler produces code "in the same league as
expert assembly-language programmers".  

BTW, has anybody seen a similar book discussing the 8087?

Eli