Compartir a través de


difftime, _difftime32, _difftime64

Finds the difference between two times.

double difftime( 
   time_t timer1,
   time_t timer0 
);
double _difftime32( 
   __time32_t timer1,
   __time32_t timer0 
);
double _difftime64( 
   __time64_t timer1,
   __time64_t timer0 
);

Parameters

  • timer1
    Ending time.

  • timer0
    Beginning time.

Return Value

difftime returns the elapsed time in seconds, from timer0 to timer1. The value returned is a double precision floating-point number. The return value may be 0, indicating an error.

Remarks

The difftime function computes the difference between the two supplied time values timer0 and timer1.

The time value supplied must fit within the range of time_t. time_t is a 64-bit value. Thus, the end of the range was extended from 03:14:07 January 19, 2038 to 23:59:59, December 31, 3000. The lower range of time_t is still midnight, January 1, 1970.

difftime is an inline function that evaluates to either _difftime32 or _difftime64 depending on whether _USE_32BIT_TIME_T is defined. _difftime32 and _difftime64 can be used directly to force the use of a particular size of the time type.

These functions validate their parameters. If either of the parameters is zero or negative, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return 0 and set errno to EINVAL.

Requirements

Routine

Required header

difftime

<time.h>

_difftime32

<time.h>

_difftime64

<time.h>

For additional compatibility information, see Compatibility in the Introduction.

Example

// crt_difftime.c
// This program calculates the amount of time
// needed to do a floating-point multiply 500 million times.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main( void )
{
   time_t   start, finish;
   long loop;
   double   result, elapsed_time;

   printf( "Multiplying 2 floating point numbers 500 million times...\n" );
   
   time( &start );
   for( loop = 0; loop < 500000000; loop++ )
      result = 3.63 * 5.27; 
   time( &finish );

   elapsed_time = difftime( finish, start );
   printf( "\nProgram takes %6.0f seconds.\n", elapsed_time );
}
Multiplying 2 floating point numbers 500 million times...

Program takes      5 seconds.

.NET Framework Equivalent

System::DateTime::Subtract

See Also

Reference

Floating-Point Support

Time Management

time, _time32, _time64