-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeval_diff.c
More file actions
25 lines (22 loc) · 848 Bytes
/
timeval_diff.c
File metadata and controls
25 lines (22 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdlib.h>
#ifdef _MSC_VER
#include <winsock2.h>
#else
#include <sys/time.h>
#endif
/* https://www.linuxquestions.org/questions/programming-9/how-to-calculate-time-difference-in-milliseconds-in-c-c-711096/#post3475339 */
long long timeval_diff(struct timeval *difference, struct timeval *end_time,
struct timeval *start_time)
{
struct timeval temp_diff;
if (difference == NULL)
difference = &temp_diff;
difference->tv_sec = end_time->tv_sec - start_time->tv_sec;
difference->tv_usec = end_time->tv_usec - start_time->tv_usec;
/* Using while instead of if below makes the code slightly more robust. */
while (difference->tv_usec < 0) {
difference->tv_usec += 1000000;
difference->tv_sec -= 1;
}
return 1000000LL * difference->tv_sec + difference->tv_usec;
}