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

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).  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?

-Tim Wojtaszek



Tue, 22 Aug 2000 03:00:00 GMT  
 *****Questions on <time.h>

: 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).  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?

clock() returns processor time in some units known only to itself; as you
say, you need CLOCKS_PER_SECOND to get seconds.  time_t is a type large
enough to contain the current calendar time, usually the number of seconds
since 1 Jan 70.  difftime() has time_t arguments because it deals in
calendar time.

Will



Thu, 24 Aug 2000 03:00:00 GMT  
 *****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).  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?

> -Tim Wojtaszek

They don't relate at all. time() gives you the current time as good as
the implementation can provide it, in a suitable arithmetic type called
time_t.
clock() gives you the implementation's best approximation of the
duration of your program's execution (that is: CPU usage).

Now if your program runs for 3 more seconds that doesn't necessarily
mean it uses the CPU for 3 seconds - it might well have been blocked or
sleeping or whatever, so the results of clock() and time() may quite
easily diverge as time passes.

If you want to profile parts of your program, clock() may be the better
choice because it measures execution time; it's often also of a finer
granularity than time().

Greetings,
        Bernd



Thu, 24 Aug 2000 03:00:00 GMT  
 *****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;

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


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



Thu, 24 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. <<<>>>Need C code advice with functions and sorting.<<<>>>

4. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

5. >>>Windows Service<<<

6. Question: <time.h>

7. <time.h> question

8. proposal: <basic.h>, <pascal.h>, <fortran.h>, <cobol.h>

9. <<<< C Grammar for yacc needed >>>>

10. - - - = = =<><> W e b - U S A <><>= = = - - -

11. >>>> * Get keypress * <<<<

12. SOURCE CODE FOR <sys/time.c> and <sys/time.c>

 

 
Powered by phpBB® Forum Software