Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

    The type, precision, and range of both time_t and clock_t are implementation defined. Local time zone and daylight savings time are also implementation defined. The Unix time standard can also vary slightly.. IE, the type of time_t and clock_t are precisely "It's a number guys!". It is therefore important to be very careful when using time in C because assumptions can lead to problems ranging from errors in program timing to possible overflow from invalid type conversions.

...

    According to the C99 standard "The clock function returns the implementation's best approximation to the processor time used by the program since the beginning of an implementation-de?ned era related only to the program invocation. To determine the time in seconds, the value returned by the clock function should be divided by the value of the macro CLOCKS_PER_SEC..." However, the question remains as to what the proper returned type of this operation should be. If you wish to determine how long a certain process required to operate, the C99 standard recommends code of the following form:

Code Block
 clock clock_t begin, end;
mystery_t mystery; 
begin=clock();
/* run process */
end=clock();
mystery = (end-begin)/CLOCKS_PER_SEC SEC

    The type of the macro CLOCKS_PER_SEC is traditionally an integer, however, the C99 standard makes no statement on its type. There is no guarantee as to the type or behavior of clock_t when divided by CLOCK_PER_SEC other than it is an arithmetic type. It is possible for longer processes to check by the standard resolution of time_t as follows:

Code Block
time_t begin, end;
time_t difference;
start=time();
/* run process */
end =time();
difference=end-start; 

    The type of time_t-time_t is in fact time_t, and after this point you can convert the difference into a more palatable format for human parsing. However, you are limited to the resolution of time_t, which is probably considerably less useful than the resolution of clock_t.

...