Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Standard library header <ctime>
3.
Syntax of difftime()
4.
Parameters
5.
Example Application
5.1.
Example 1:
5.2.
Output 
5.3.
Example 2:
5.4.
Output 
5.5.
Example 3:
5.6.
Output 
6.
Frequently Asked Questions
6.1.
What exactly is difftime()?
6.2.
What does Difftime return?
6.3.
What is the time_t type in C?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

difftime() function in C++

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This blog will look at the difftime() function in C++. The difftime() function in C++ calculates the difference in seconds between two times. The <ctime> header file defines the difftime() function. 

The ctime header file defines the difftime() function. The difftime() method is used to find the difference in seconds between two times. 

The difftime() function is a built-in C++ function that is defined in the header file. The function receives two time_t type inputs and calculates the difference between them.

We'll go over the entire architecture and features of difftime() in the following chapters.

Also see, Literals in C, Fibonacci Series in C++

Standard library header <ctime>

The C++ <ctime> header file declares a set of date and time methods, macros, and types. The time() function, for example, is used to obtain the current time.

Initially, this header was known as <time.h> in the C standard library.

The C-style date and time library includes this header.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.

Recommended Readings:

Happy Programming!

Previous article
wcslen() function in C++
Next article
fflush in C
Live masterclass