*****Questions on <time.h>

Quote:

>I am trying to time some parts of my program and using the <time.h>

>header functions. i planned on using the clock(); function at the

>begining and at the end and then subtracting and dividing by

>CLOCKS_PER_SECOND(this is from k&r2).

That should be CLOCKS_PER_SEC

Quote:

> Now the clock function returns a

>number of the type clock_t, and another function difftime(time_t time1,

>time_2 time2); returns a double of time1-time2. how do clock_t and

>time_t relate to each other?

They don't relate to each other at all. time_t is designed to hold calendar

dates and times and there are a variety if functions in <time.h> to

manipulate time_t values. clock() is somewhat independent of the other

functions in <time.h> and is designed to measure time intervals (specifically

CPU execution time). To portably get the difference in seconds between two

time_t values you should use difftime(). For clock_t values you can subtract

the values directly and divide by CLOCKS_PER_SEC. It is wise to perform the

division using floating point arithmetic so that any fractions don't get

truncated (since clock_t may be a integer type).

clock_t start, end;

double interval;

start = clock();

/* Put the code you want to measure here */

end = clock();

interval = (double)(end - start) / CLOCKS_PER_SEC;

--

-----------------------------------------

-----------------------------------------