Important: The information in this document is obsolete and should not be used for new development.
Computing Elapsed Time
In the revised and extended Time Managers, theRmvTimeprocedure returns, in thetmCountfield of the task record, a value representing any unused time. This feature makes the Time Manager extremely useful for computing elapsed times.To compute the amount of time that a routine takes to run, call
PrimeTimeat the beginning of the interval to be measured and specify a delay greater than the expected elapsed time. Then callRmvTimeat the end of the interval and subtract the unused time returned intmCountfrom the original delay passed toPrimeTime.To obtain the most accurate results, you should calculate all times in microseconds (in which case the
tmCountfield of the task record has a range of about 35 minutes). To get an exact measurement, compute the overhead associated with calling the Time Manager and subtract it from the preliminary result. Listing 3-5 illustrates a technique for calculating that overhead.Listing 3-5 Calculating the time required to install and activate a Time Manager task
FUNCTION TMOverhead: LongInt; VAR myTask: TMTask; {a Time Manager task record} myStart: LongInt; {initial delay passed to PrimeTime} myElapsed: LongInt; {elapsed time} BEGIN myStart := -(MAXLONG); {use a large negative number} WITH myTask DO {set up the task record} BEGIN tmAddr := NIL; {no task to execute} tmWakeUp := 0; tmReserved := 0; END; InsTime(@myTask); {install the task} PrimeTime(@myTask, myStart); {prime the task} RmvTime(@myTask); {remove the task} myElapsed := myStart - myTask.tmCount; TMOverhead := -(myElapsed); {the elapsed time} END;TheTMOverheadfunction defined in Listing 3-5 sets up a Time Manager task record with no completion routine. In this case, you can allocate the task record as a local variable on the stack because the task record is removed before the function exits. Then the task is activated by callingPrimeTimewith a very large negative value. (The negative value represents microseconds.) Immediately the task is deactivated and removed. The function determines the elapsed time by subtracting the value returned in thetmCountfield of the task record from the original delay time.Listing 3-6 illustrates how to measure the elapsed time associated with a request to delay program execution by 1 tick.
Listing 3-6 Calculating the time consumed by a 1-tick delay
FUNCTION CheckDelayTiming: LongInt; VAR myTask: TMTask; {a Time Manager task record} myStart: LongInt; {initial delay passed to PrimeTime} myEnd: LongInt; {unused time} myTicks: LongInt; {ignored; needed for Delay procedure} myElapsed: LongInt; {elapsed time} BEGIN myStart := -(MAXLONG); {use a large negative number} WITH myTask DO {set up the task record} BEGIN tmAddr := NIL; {no task to execute} tmWakeUp := 0; tmReserved := 0; END; InsTime(@myTask); {install the task} PrimeTime(@myTask, myStart); {prime the task} Delay(1, myTicks); {delay for 1 tick} RmvTime(@myTask); {remove the task} myEnd := myTask.tmCount; {get unused part of myStart} IF myEnd < 0 THEN {myEnd is in microseconds} myElapsed := ABS(myStart - myEnd) - TMOverhead ELSE {myEnd is in milliseconds} myElapsed := ABS(myStart + (myEnd * 1000)) - TMOverhead; CheckDelayTiming := myElapsed;{the elapsed time} END;TheCheckDelayTimingfunction is similar to theTMOverheadfunction except that the section of code to be timed occurs between the calls toPrimeTimeandRmvTime. TheCheckDelayTimingfunction simply times a call to theDelayprocedure with a 1-tick delay time. OnceDelayhas completed and the task record has been deactivated,CheckDelayTimingdetermines whether the unused time returned in thetmCountfield represents microseconds or milliseconds. The value returned byCheckDelayTimingis in microseconds.