The difftime() function is defined in <ctime> header file.
difftime() prototype
double difftime(time_t end,time_t begin);
The difftime() takes two time_t
objects: end and begin, and computes the difference as end - begin
and return the result in seconds.
If end refers to the time which occur before begin then the result is negative.
difftime() Parameters
- end: Represent the end time.
- begin: Represent the beginning time.
difftime() Return value
- The difftime() function returns the difference in time between end and begin in seconds.
Example: How difftime() function works
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t start, finish;
long product;
time(&start);
for(int i=0; i<10000; i++)
{
for(int j=0; j<100000; j++)
{
product = i*j;
}
}
time(&finish);
cout << "Time required = " << difftime(finish, start) << " seconds";
return 0;
}
When you run the program, the output will be:
Time required = 3 seconds
Here, time() function is used to get the current calendar time of type time_t
.
Also Read: