Syntax of difftime()
double difftime(time_t end,time_t begin);
The difftime() function takes two time_t objects, end and begin, and computes the difference between them as end - begin, returning the result in seconds.
The result is negative if the end relates to the period that occurs before begin.
Parameters
There are two parameters that this function accepts:
- Begin: time_t object that represents the start time.
- End: a time_t object that represents the end time.
Return Value: The difference in time between end and begin is returned by the difftime() function in seconds.
Example Application
Example 1:
The following program demonstrates the above function: -
// C++ program to demonstrate
// example of difftime() function.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t begin, end;
long long prod;
time(&begin);
for(int i=0; i<10000; i++)
{
for(int j=0; j<100000; j++)
{
prod = i*j;
}
}
time(&end);
cout << "Time required = " << difftime(end, begin) << " seconds";
return 0;
}
Output
When you run the program, you will get the following results:
Time required = 3 seconds
--------------------------------
Process exited after 2.417 seconds with return value 0
Example 2:
The following program demonstrates the above function: -
// C++ program to demonstrate
// example of difftime() function.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t begin, end;
long long add;
time(&begin);
for (int i = 0; i < 20000; i++) {
for (int j = 0; j < 20000; j++);
}
for (int i = 0; i < 20000; i++) {
for (int j = 0; j < 20000; j++);
}
for (int i = 0; i < 20000; i++) {
for (int j = 0; j < 20000; j++);
}
time(&end);
cout << "Total time required = "
<< difftime(end, begin)
<< " seconds " << endl;
return 0;
}
Output
When you run the program, you will get the following results:
Total time required = 2 seconds
--------------------------------
Process exited after 2.618 seconds with return value 0
Example 3:
The following program demonstrates the above function: -
// C++ program to demonstrate
// example of difftime() function.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t present;
struct tm ny;
double sec;
//fetch live time
time(&present);
ny = *localtime(&present);
ny.tm_hour = 0; ny.tm_min = 0; ny.tm_sec = 0;
ny.tm_mon = 0; ny.tm_mday = 1;
sec = difftime(present,mktime(&ny));
cout<<sec<<" seconds since new year in the current timezone."<<endl;
return 0;
}
Output
When you run the program, you will get the following results:
1.2813e+07 seconds since new year in the current timezone.
--------------------------------
Process exited after 0.08575 seconds with return value 0
Try and compile with online c++ compiler.
Frequently Asked Questions
What exactly is difftime()?
The difftime() function in C++ calculates the difference in seconds between two times. The <ctime> header file defines the difftime() function.
What does Difftime return?
As a double-precision integer, the difftime() method delivers the elapsed time in seconds from time1 to time2. In <time>, the type time t is defined.
What is the time_t type in C?
The time t datatype is an ISO C library data type that is used to store system time values. The normal time() library function returns such numbers. This type is defined in the standard <time. h> header as a typedef.
Conclusion
In this blog, we have extensively discussed the difftime() function in C++. We hope that this article has provided you with additional information about the time difference function. And to learn in-depth about c++ functions, check out the course on our c++ function on the Coding Ninjas website. And also, check out the excellent content on the Coding Ninjas Website, Android Development, Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.
Recommended Readings:
Happy Programming!