-
Notifications
You must be signed in to change notification settings - Fork 7
/
chr_time.c
148 lines (134 loc) · 3.47 KB
/
chr_time.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "config.h"
#include "stdlib.h"
#include "chr_time.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#include <time.h>
#include <sys/timeb.h>
#endif
extern void
chr_get_time( chr_time *time)
{
#ifdef HAVE_GETTIMEOFDAY
gettimeofday((struct timeval*)time, NULL);
#else
/* GSE... No gettimeofday on windows.
* Must use _ftime, get millisec time, convert to usec. Bleh.
*/
struct _timeb nowb;
_ftime(&nowb);
((struct timeval*)time)->tv_sec = (long)nowb.time;
((struct timeval*)time)->tv_usec = nowb.millitm * 1000;
#endif
// gettimeofday((struct timeval *) time, NULL);
}
extern void
chr_timer_start( chr_time *time)
{
chr_get_time(time);
}
extern void
chr_timer_stop( chr_time *time)
{
struct timeval now;
struct timeval duration;
#ifndef HAVE_WINDOWS_H
gettimeofday((struct timeval*)&now, NULL);
#else
/* GSE... No gettimeofday on windows.
* Must use _ftime, get millisec time, convert to usec. Bleh.
*/
struct _timeb nowb;
_ftime(&nowb);
((struct timeval*)&now)->tv_sec = (long)nowb.time;
((struct timeval*)&now)->tv_usec = nowb.millitm * 1000;
#endif
// gettimeofday(&now, NULL);
chr_timer_diff((chr_time*)&duration, (chr_time*)&now, time);
*((struct timeval *) time) = duration;
}
extern int
chr_timer_eq_zero (chr_time *time)
{
struct timeval *t = (struct timeval *) time;
return ((t->tv_sec == 0) && (t->tv_usec == 0));
}
extern void
chr_timer_diff( chr_time *diff, chr_time *src1, chr_time *src2)
{
struct timeval d;
struct timeval *s1 = (struct timeval *)src1;
struct timeval *s2 = (struct timeval *)src2;
d.tv_sec = s1->tv_sec - s2->tv_sec;
d.tv_usec = s1->tv_usec - s2->tv_usec;
if (d.tv_usec < 0) {
d.tv_usec += 1000000;
d.tv_sec--;
}
*((struct timeval*)diff) = d;
}
extern void
chr_timer_sum( chr_time *sum, chr_time *src1, chr_time *src2)
{
struct timeval s;
struct timeval *s1 = (struct timeval *)src1;
struct timeval *s2 = (struct timeval *)src2;
s.tv_sec = s1->tv_sec + s2->tv_sec;
s.tv_usec = s1->tv_usec + s2->tv_usec;
if (s.tv_usec > 1000000) {
s.tv_usec -= 1000000;
s.tv_sec++;
}
*((struct timeval*)sum) = s;
}
extern double
chr_time_to_secs(chr_time *time)
{
return (double)((struct timeval*)time)->tv_sec +
((double)((struct timeval*)time)->tv_usec)/1000000.0;
}
extern double
chr_time_to_millisecs(chr_time *time)
{
return ((double)((struct timeval*)time)->tv_sec)*1000.0 +
((double)((struct timeval*)time)->tv_usec)/1000.0;
}
extern double
chr_time_to_microsecs(chr_time *time)
{
return ((double)((struct timeval*)time)->tv_sec)*1000000.0 +
((double)((struct timeval*)time)->tv_usec);
}
extern double
chr_time_to_nanosecs(chr_time *time)
{
return ((double)((struct timeval*)time)->tv_sec)*1000000000.0 +
((double)((struct timeval*)time)->tv_usec*1000.0);
}
extern double
chr_approx_resolution()
{
struct timeval diff;
#ifndef HAVE_WINDOWS_H
struct timeval start, stop;
gettimeofday(&start, NULL);
gettimeofday(&stop, NULL);
while(start.tv_usec == stop.tv_usec) {
gettimeofday(&stop, NULL);
}
chr_timer_diff((chr_time*)&diff, (chr_time*)&stop, (chr_time*)&start);
#else
struct _timeb start, stop;
_ftime(&start);
_ftime(&stop);
while (start.millitm == stop.millitm) {
_ftime(&stop);
}
diff.tv_sec = 0;
diff.tv_usec = (stop.millitm - start.millitm) * 1000;
#endif
return chr_time_to_secs((chr_time*)&diff);
}