Difftime function in C++

While calculating or performing any operation, we might need any function to calculate the total time that has passed until now or provide the difference of time in seconds between two points. In C++, there can be different ways to perform this task inappropriately. But we will discuss the built-in function of C++, that is difftime() function, As the name indicates that it calculates the difference in time. This function is defined in the library <ctime>. So whenever we are willing to calculate time, we will need this library in the source code.

Syntax of difftime()

double difftime (time_obj end, time_obj start);

The function takes two parameters in it. These are the objects that are declared through the object of time ‘time_obj’. One is the ‘end’ that shows the ending time of any process. At the same time, the second one is the ‘start’ that represents the starting time. The returning value of the difftime() function returns the difference between the time in seconds obtained from the two objects as described above.

This function is associated with the built-in function time() in C++, as this function returns the current calendar time. This is also present in the header file of C++.

Implementation of difftime() function

Example 1 # Difftime() for the products

To find the difference between two times in seconds, it is necessary to add the library in which the difftime() function is defined. In the C++ programming language, this library must be used.

#include <iostream>

#include <ctime>

As discussed earlier, in the main program, two objects, start and finish, are necessary to calculate the difference between times. These two objects are declared by using time_time.

Time_t start, finish

Another variable with the name product of a long data type is declared that will store the operation results in it. Whenever the logic we want to apply in the program, this will be written inside the body of start and finish objects.



Because the start time object starts the time and continues while the function/ operation is in progress when the process is finished, the finished object notes the time of ending. For example, in this program explained below, we have used a simple nested ‘for’ loop to calculate the product of two values. These two values start from 0 and end at a large value. The outer ‘for’ loop ends before 1000, and the inner loop ends at 30000; inside the ‘for’ loop, the body in each iteration, the number from the outer and the number from the inner loop are multiplied. The variables ‘I’ and ‘j’ are denoted to each number after each iteration, a new number in each cycle. The resultant is stored in the ‘product’ variable

Product = I * j;

The required time we want to obtain is calculated through the difftime function. As this function contains two parameters in it, so the function will be:

Difftime(finish, start);

This function will return the time obtained by subtracting the start time from the finish time.

Now save the source code with the c extension. We use a g++ compiler to compile a C++ file and execute it. The file we used here is ‘dif. c’ that contains the source code, it is compiled, and the output is stored in the output file through ‘-o’.

$ g++ -o dif dif.c

$ ./dif

You can see that the required time is 0 seconds. It means that the nested loop will calculate the product in 0 seconds. In the above code, the value in the outer for loop is smaller than the inner one, so the time difference was zero because it can be calculated easily. What if the value in the inner for loop becomes smaller than the outer for loop? We will explain hereafter. The rest of the requirements are the same. The product will be calculated inside the loop. We will use the start and finish objects to note the starting and ending values.

Now save the code and run it. On execution, you will see that it will take some time. As compared to the previous situation, that takes only zero seconds, and the result was displayed abruptly, but in this case, it seems that there must be more than zero seconds.

After some wait, you will see the resultant statement. According to this, the loops took 62 seconds to get executed so that the product of two values during each iteration can be calculated.

Example 2 # Difftime() for nested loops

Unlike the previous statement, we have used a different library here.


There is no need to specify ‘iostream’ and ‘ctime’ or any other library using this library. Both the variables are initialized. In this example, we have used nested for loops three times. Each nested loop ends, and the next nested loop starts. Each loop inside the nested loop has a different value for variable I; for the j variable, the values are the same for the inner loop.

Moreover, we have not calculated anything or any operation. Only the execution of the loops had taken place. Starting and ending variables will observe the starting and the ending time.

After each execution, in the end, the difftime () function is called to execute the answer.

Difftime(ending, start)

We will execute the source code to see the seconds consumed in the execution of three nested loops.

It will take 10 seconds at all for all three loops collectively.

Example 3 # Difftime() for a time between dates

To calculate the time difference from the start of the program till it ends is elaborated already. Our concern of discussion is to calculate the difference in seconds from one specific day until now. Here the dates are not mentioned properly. But the program will fetch the current date from the system. Here we will calculate the difference in time in seconds from the new year day till now.

The object of the time library is used to fetch the current time so that the difference can be calculated. Here we don’t need the start and end variables separately.

A structure of time ‘new year’ is initialized here. We will use the ‘now’ variable to get the current time.


The object ‘tm’ of the new year structure will declare the hours, min, seconds, and month as zero. The function difftime will be called that will take now (current time), and another built-in function inside the parameter to get the new year time in seconds.

Seconds = difftime(now,mktime(&newyear));

Now execute the program; you will see that 6036632 seconds are passed till now according to the current date of your system.


‘difftime () C++’ is a function that deals with fetching the time in seconds obtained by subtracting the time of starting from the time of ending; these two variables of a time object type are used to provide time in seconds. Now wrapping up the article, we will talk about some examples that we have included in the article. This function also helps in providing the time from a specific date till now, as discussed above in the example.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.