Time Difference Calculation in CPP

Spread the love


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 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.



Venkatesh Macha

Hi Guys, I am Venkatesh Macha. Founder of SillyCodes. I am a programmer, Presently working as a Software Engineer in Hyderabad. I love Linux and Open Source. Apart from that, I love to explore new technologies and things.

You may also like...

Leave a Reply