http://www.math.uic.edu/~jan/MCS275/lec41.htmlBelow is the listing of the main program (stored in
mytimer.c ),
that contains all routines in one file :
/* L-41 MCS 275 Fri 20 Apr 2001 : timing C programs */
/* The program below compares the time for multiplying two floats
(single precision) with multiplying two doubles (double precision).
Because one multplication happens extremely fast (unnoticable),
we execute many multiplications. We can test the correctness
of the timing by executing the program on the prompt like
prompt> time ./a.out
The UNIX command time displays after a.out terminates the
user time (among other things). */
#include <stdio.h>
#include <time.h>
#define N 100000000
typedef struct timedata timer;
struct timedata
{
clock_t user_time;
time_t real_time;
};
void tstart ( timer *tmr );
/* initializes the timer */
void tstop ( timer *tmr );
/* stops the timer */
long usersec ( timer tmr );
/* returns elapsed seconds user cpu time */
long realsec ( timer tmr );
/* returns elapsed seconds real time */
long usermsc ( timer tmr );
/* returns elapsed milliseconds user cpu time */
long realmsc ( timer tmr );
/* returns elapsed milliseconds real time */
void tprint ( timer tmr );
/* formatted print of user cpu time, seconds and milliseconds */
int main(void)
{
long i;
timer tmr,total;
float a, b = 3.3333, c = 5.5555;
double x, y = 3.3333, z = 5.5555;
tstart(&total);
printf("\nTiming single precision float multiplication : \n");
tstart(&tmr);
for (i = 0; i < a =" b*c;" i =" 0;" x =" y*z;">user_time = clock();
tmr->real_time = time(NULL);
}
void tstop ( timer *tmr )
{
tmr->user_time = clock() - tmr->user_time;
tmr->real_time = time(NULL) - tmr->real_time;
}
long usersec ( timer tmr )
{
return tmr.user_time/CLOCKS_PER_SEC;
}
long realsec ( timer tmr )
{
return tmr.real_time;
}
long usermsc ( timer tmr )
{
return (tmr.user_time*1000)/CLOCKS_PER_SEC;
}
long realmsc ( timer tmr )
{
return (tmr.real_time*1000);
}
void tprint ( timer tmr )
{
long usec = usersec(tmr);
long umsc = usermsc(tmr);
long rsec = realsec(tmr);
long rmsc = realmsc(tmr);
printf("%s%2d%s%3d%s\n",
"Elapsed CPU user time : ", usec, " second(s) and ",
umsc - 1000*usec , " millisecond(s).");
printf("%s%2d%s%3d%s\n",
"Elapsed real time : ", rsec, " second(s) and ",
rmsc - 1000*rsec , " millisecond(s).");
}
If we put all the timing utilities in the library
libtimer ,
then our main program
usetimer.c looks like
/* L-41 MCS 275 Fri 20 Apr 2001 : timing C programs with library */
#include <stdio.h>
#include "libtimer.h"
#define N 100000000
int main(void)
{
long i;
timer tmr,total;
float a, b = 3.3333, c = 5.5555;
double x, y = 3.3333, z = 5.5555;
tstart(&total);
printf("\nTiming single precision float multiplication : \n");
tstart(&tmr);
for (i = 0; i < a =" b*c;" i =" 0;" x =" y*z;">
Then libtimer.h has to contain
/* L-41 MCS 275 Fri 20 Apr 2001 : creating a library for timing */
#include <time.h>
typedef struct timedata timer;
struct timedata
{
clock_t user_time;
time_t real_time;
};
void tstart ( timer *tmr );
/* initializes the timer */
void tstop ( timer *tmr );
/* stops the timer */
long usersec ( timer tmr );
/* returns elapsed seconds user cpu time */
long realsec ( timer tmr );
/* returns elapsed seconds real time */
long usermsc ( timer tmr );
/* returns elapsed milliseconds user cpu time */
long realmsc ( timer tmr );
/* returns elapsed milliseconds real time */
void tprint ( timer tmr );
/* formatted print of user cpu time, seconds and milliseconds */
and the file libtimer.c has as content
/* L-41 MCS 275 Fri 20 Apr 2001 : timing library */
/* If this file has the name libtimer.c, we can create the library
by typing after the prompt :
prompt> gcc -c libtimer.c
prompt> ar ruv libtimer.a libtimer
or with we place in makefile the following :
libtimer: libtimer.c
gcc -c libtimer.c
ar ruv libtimer.a libtimer.o
*/
#include <stdio.h>
#include <time.h>
#include "libtimer.h"
void tstart ( timer *tmr )
{
tmr->user_time = clock();
tmr->real_time = time(NULL);
}
void tstop ( timer *tmr )
{
tmr->user_time = clock() - tmr->user_time;
tmr->real_time = time(NULL) - tmr->real_time;
}
long usersec ( timer tmr )
{
return tmr.user_time/CLOCKS_PER_SEC;
}
long realsec ( timer tmr )
{
return tmr.real_time;
}
long usermsc ( timer tmr )
{
return (tmr.user_time*1000)/CLOCKS_PER_SEC;
}
long realmsc ( timer tmr )
{
return (tmr.real_time*1000);
}
void tprint ( timer tmr )
{
long usec = usersec(tmr);
long umsc = usermsc(tmr);
long rsec = realsec(tmr);
long rmsc = realmsc(tmr);
printf("%s%2d%s%3d%s\n",
"Elapsed CPU user time : ", usec, " second(s) and ",
umsc - 1000*usec , " millisecond(s).");
printf("%s%2d%s%3d%s\n",
"Elapsed real time : ", rsec, " second(s) and ",
rmsc - 1000*rsec , " millisecond(s).");
}
To create the library and the executables, use the makefile below :
libtimer.a: libtimer.h libtimer.c
gcc -c libtimer.c
ar ruv libtimer.a libtimer.o
mytimer: mytimer.c
gcc -o mytimer mytimer.c
usetimer: usetimer.c libtimer.a
gcc -o usetimer usetimer.c libtimer.a
clean:
/bin/rm -f -r *o libtimer.a mytimer usetimer