C <time.h> - difftime() Function
The C <time.h> difftime() function compute difference between two time_t objects as (time_end - time_beg) in seconds. If time_end refers to time point before time_beg, then the function returns negative result.
Syntax
double difftime (time_t time_end, time_t time_beg);
Parameters
time_beg |
Specify lower bound of the time interval whose length is calculated. |
time_end |
Specify upper bound of the time interval whose length is calculated. |
Return Value
Returns (time_end - time_beg) in seconds.
Example:
The example below shows the usage of difftime() function.
#include <stdio.h> #include <time.h> int main (){ time_t start, finish; long product; time(&start); for(int i = 0; i < 100000; i++) for(int j = 0; j < 25000; j++) product = i*j; time(&finish); //calculating the time difference printf("Time taken = %f seconds", difftime(finish, start)); return 0; }
The possible output of the above code could be:
Time taken = 6.000000 seconds
❮ C <time.h> Library