difftime() function in C++

The difftime() function is defined in ctime header file. The difftime() function is used to calculate the difference between two times in second.


double difftime(time_t end, time_t start);

Parameters: This method accepts two parameters:

  • start: time_t object for start time.
  • end: time_t object for end time.

Returns: This function returns the difference between two times in seconds.






// C++ program to demonstrate
// example of difftime() function.
#include <bits/stdc++.h>
using namespace std;
int main()
    time_t start, ending;
    long addition;
    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++);
    cout << "Total time required = "
         << difftime(ending, start)
         << " seconds " << endl;
    return 0;



Total time required = 2 seconds

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.