Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Processes
Chapter 3 - Time Manager / Using the Time Manager


Computing Elapsed Time

In the revised and extended Time Managers, the RmvTime procedure returns, in the tmCount field 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 PrimeTime at the beginning of the interval to be measured and specify a delay greater than the expected elapsed time. Then call RmvTime at the end of the interval and subtract the unused time returned in tmCount from the original delay passed to PrimeTime.

To obtain the most accurate results, you should calculate all times in microseconds (in which case the tmCount field 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;
The TMOverhead function 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 calling PrimeTime with 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 the tmCount field 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;
The CheckDelayTiming function is similar to the TMOverhead function except that the section of code to be timed occurs between the calls to PrimeTime and RmvTime. The CheckDelayTiming function simply times a call to the Delay procedure with a 1-tick delay time. Once Delay has completed and the task record has been deactivated, CheckDelayTiming determines whether the unused time returned in the tmCount field represents microseconds or milliseconds. The value returned by CheckDelayTiming is in microseconds.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
17 JUN 1996