diffrence of two time in c++
19,949
Solution 1
Use difftime
:
double diff = difftime(t2, t1);
This gives you the difference in seconds. Multiply diff
by 1000
to get milliseconds.
Solution 2
time()
returns an integer type giving the time since the epoch in seconds, therefore you will never get millisecond resolution. Use gettimeofday()
instead, that gives a struct with seconds and microseconds:
struct timeval t1, t2;
gettimeofday(&t1, NULL);
/* ... */
gettimeofday(&t2, NULL);
int milliSeconds = (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec)/1000;
Solution 3
Here is what you need: http://www.cplusplus.com/reference/clibrary/ctime/difftime/
Author by
Masoud
From Software Developer to Social Worker and now back to Cyber Security.
Updated on June 13, 2022Comments
-
Masoud almost 2 years
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