17 Clocks
17 Clocks
CS 241 Lecture 17
R: Ch 9, pp 300-307+man pages
for gprof
Roy Campbell
Extensions
POSIX:XSI microseconds
POSIX:TMR nanoseconds
int main(void) {
time_t tstart;
tstart = time(NULL);
function_to_time();
printf(“function_to_time took %f seconds of elapsed time\n”,
difftime(time(NULL), tstart));
return(0);
}
#include <sys/time.h>
void function_to_time(void);
int main(void) {
long timedif;
struct timeval tpend;
struct timeval tpstart;
if (gettimeofday(&tpstart, NULL)) {
fprintf(stderr, “Failed to get start time\n”);
return 1;
}
int main(void) {
int i;
int numcalls = 1;
int numdone = 0;
long sum = 0;
long timedif[NUMDIF];
struct timeval tlast;
struct timeval tthis;
if (gettimeofday(&tlast, NULL)) {
fprintf(stderr, “Failed to get gettimeofday\n”);
return 1;
}
08/13/24 CS241 © 2005 Roy Campbell, All R 20
ights Reserved
A program to test the resolution of
gettimeofday
while (numdone < NUMDIF) {
numcalls++;
if (gettimeofday(&tthis, NULL)) {
fprintf(stderr, “Failed to get a later gettimeofday.\n”);
return 1;
}
timedif[numdone] = MILLION*(tthis.tv_sec – tlast.tv_sec) +
tthis.tv_usec –tlast.tv_usec;
if (timedif[numdone] != 0 {
numdone++;
tlast=this;
}
}
………………….