
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
difftime() function in C++
In this article we are going to discuss the difftime() function in C++, its syntax, working and its return values.
difftime() function is an inbuilt function in C++ which is defined in header file. The function accepts two parameters of time_t type, function calculate the difference between the two times
Syntax
double difftime(time_t end, time_t beginning);
Return value
Returns the difference of the time in seconds, stored as double data type.
Example
#include <stdio.h> #include <time.h> int main () { time_t now; struct tm newyear; double seconds; time(&now); /* get current time; */ newyear = *localtime(&now); newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0; newyear.tm_mon = 0; newyear.tm_mday = 1; seconds = difftime(now,mktime(&newyear)); printf ("%.f seconds since new year in the current timezone.\n", seconds); return 0; }
Output
If we run the above code it will generate the following output −
3351041 seconds since new year in the current timezone.
Example
#include <iostream> #include <ctime> using namespace std; int main() { time_t start, ending; long addition; time(&start); for (int i = 0; i < 50000; i++) { for (int j = 0; j < 50000; j++); } for (int i = 0; i < 50000; i++) { for (int j = 0; j < 50000; j++); } for (int i = 0; i < 50000; i++) { for (int j = 0; j < 50000; j++); } time(&ending); cout << "Total time required = " << difftime(ending, start) << " seconds " << endl; return 0; }
Output
If we run the above code it will generate the following output −
Total time required = 37 seconds
- Related Articles
- Difftime() C library function
- Function returning another function in JavaScript
- Function Expression vs Function Declaration in JavaScript?
- Function pointer to member function in C++
- Implementing custom function like String.prototype.split() function in JavaScript
- Implementing a custom function like Array.prototype.filter() function in JavaScript
- In MySQL, how FIELD() function is different from FIND_IN_SET() function?
- strcmp() function in PHP
- strcoll() function in PHP
- strcspn() function in PHP
- strip_tags() function in PHP
- stripcslashes() function in PHP
- stripos() function in PHP()
- stristr() function in PHP
- stripslashes() function in PHP

Advertisements