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
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.
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.
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.
Use difftime
:
double diff = difftime(t2, t1);
This gives you the difference in seconds. Multiply diff
by 1000
to get milliseconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With