Time Difference Calculation in CPP


Calculate Time difference in C and CPP:

We can use clock() function from the ctime  library to calculate time difference between the two events.

The function clock_t clock(void) returns the number of clock ticks elapsed since the program was launched.

Program to calculate the Elapsed time (Time Difference) of task:

Output :

Explanation :

First of all, We are storing the initial time in the begin_time  variable using the clock()  function.

Then I’m printing the number of clock ticks for the seconds using CLOCKS_PER_SEC .  Just for the logging purpose.

In the next line, I’m counting 1000000000, to create some time delay.

Finally, I’m calculating the time difference by subtracting the present time with the begin_time .

Then dividing the resultant value with the CLOCKS_PER_SEC  to get the exact elapsed time in the Seconds.


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply