User`s manual

114 digi.com The Virtual Driver
7.3 Global Timer Variables
SEC_TIMER, MS_TIMER and TICK_TIMER are global variables defined as shared unsigned
long. These variables should never be changed by an application program. Among other things, the
TCP/IP stack depends on the validity of the timer variables.
On initialization, SEC_TIMER is synchronized with the real-time clock. The date and time can be
accessed more quickly by reading SEC_TIMER than by reading the real-time clock.
The periodic interrupt updates SEC_TIMER every second, MS_TIMER every millisecond, and
TICK_TIMER 1024 times per second (the frequency of the periodic interrupt). These variables are used
by the DelaySec, DelayMS and DelayTicks functions, but are also convenient for application pro-
grams to use for timing purposes.
7.3.1 Example: Timing Loop
The following sample shows the use of MS_TIMER to measure the execution time in microseconds of a
Dynamic C integer add. The work is done in a nodebug function so that debugging does not affect tim-
ing.
#define N 10000
main(){ timeit(); }
nodebug timeit(){
unsigned long int T0;
float T2,T1;
int x,y;
int i;
T0 = MS_TIMER;
for(i=0;i<N;i++) { }
// T1 gives empty loop time
T1=(MS_TIMER-T0);
T0 = MS_TIMER;
for(i=0;i<N;i++){ x+y;}
// T2 gives test code execution time
T2=(MS_TIMER-T0);
// subtract empty loop time and convert to time for single pass
T2=(T2-T1)/(float)N;
// multiply by 1000 to convert milliseconds to microseconds.
printf("time to execute test code = %f us\n",T2*1000.0);
}