Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diffrence of two time in c++

Tags:

c++

time

At first I get a time by

time_t t1 = time(0) 

(is it right for getting current time?) then

time_t t2 = time(0)

now I want the find the difference between t1 and t2 in milliseconds I searched a lot but it didn't worked. lots of casting problems and unable to change it to milliseconds thanks for your help in advance

like image 711
Masoud Avatar asked Aug 25 '12 10:08

Masoud


People also ask

How do you find the difference between two times?

First, identify the starting and an ending time. The goal is to subtract the starting time from the ending time under the correct conditions. If the times are not already in 24-hour time, convert them to 24-hour time. AM hours are the same in both 12-hour and 24-hour time.

How do you find the difference between two numbers in C?

Using abs() – A Shortest way to find difference of two numbers. By using abs() function we can get the difference of two integer numbers without comparing them, abs() is a library function which is declared in stdlib. h – This function returns the absolute value of given integer.

How do you calculate elapsed time in C?

To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.


1 Answers

Use difftime:

double diff = difftime(t2, t1);

This gives you the difference in seconds. Multiply diff by 1000 to get milliseconds.

like image 125
Sergey Kalinichenko Avatar answered Oct 13 '22 03:10

Sergey Kalinichenko