Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

difftime() function in C++

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

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.

 Example:- 

cpp




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

Output:

Total time required = 2 seconds

Time Complexity: O(n^2) where n is 20000.

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Last Updated : 14 Mar, 2023
Like Article
Save Article
Similar Reads