forked from python/peps
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench_time.c
187 lines (167 loc) · 4.25 KB
/
bench_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Benchmark program written for the PEP 418.
*
* gcc bench_time.c -O3 -lrt -o bench_time && ./bench_time
*/
#include <time.h>
#include <stdio.h>
#include <sys/time.h>
#ifdef CLOCK_REALTIME
# define HAVE_CLOCK_GETTIME
#else
typedef int clockid_t;
#endif
#define NRUN 5
#define NLOOP 100000
#define UNROLL(expr) \
expr; expr; expr; expr; expr; expr; expr; expr; expr; expr
#define NUNROLL 10
#ifdef HAVE_CLOCK_GETTIME
typedef struct {
const char *name;
clockid_t identifier;
} CLOCK;
CLOCK clocks[] = {
#ifdef CLOCK_REALTIME_COARSE
{"CLOCK_REALTIME_COARSE", CLOCK_REALTIME_COARSE},
#endif
#ifdef CLOCK_MONOTONIC_COARSE
{"CLOCK_MONOTONIC_COARSE", CLOCK_MONOTONIC_COARSE},
#endif
#ifdef CLOCK_THREAD_CPUTIME_ID
{"CLOCK_THREAD_CPUTIME_ID", CLOCK_THREAD_CPUTIME_ID},
#endif
#ifdef CLOCK_PROCESS_CPUTIME_ID
{"CLOCK_PROCESS_CPUTIME_ID", CLOCK_PROCESS_CPUTIME_ID},
#endif
#ifdef CLOCK_MONOTONIC_RAW
{"CLOCK_MONOTONIC_RAW", CLOCK_MONOTONIC_RAW},
#endif
#ifdef CLOCK_VIRTUAL
{"CLOCK_VIRTUAL", CLOCK_VIRTUAL},
#endif
#ifdef CLOCK_UPTIME_FAST
{"CLOCK_UPTIME_FAST", CLOCK_UPTIME_FAST},
#endif
#ifdef CLOCK_UPTIME_PRECISE
{"CLOCK_UPTIME_PRECISE", CLOCK_UPTIME_PRECISE},
#endif
#ifdef CLOCK_UPTIME
{"CLOCK_UPTIME", CLOCK_UPTIME},
#endif
#ifdef CLOCK_MONOTONIC_FAST
{"CLOCK_MONOTONIC_FAST", CLOCK_MONOTONIC_FAST},
#endif
#ifdef CLOCK_MONOTONIC_PRECISE
{"CLOCK_MONOTONIC_PRECISE", CLOCK_MONOTONIC_PRECISE},
#endif
#ifdef CLOCK_REALTIME_FAST
{"CLOCK_REALTIME_FAST", CLOCK_REALTIME_FAST},
#endif
#ifdef CLOCK_REALTIME_PRECISE
{"CLOCK_REALTIME_PRECISE", CLOCK_REALTIME_PRECISE},
#endif
#ifdef CLOCK_SECOND
{"CLOCK_SECOND", CLOCK_SECOND},
#endif
#ifdef CLOCK_PROF
{"CLOCK_PROF", CLOCK_PROF},
#endif
{"CLOCK_MONOTONIC", CLOCK_MONOTONIC},
{"CLOCK_REALTIME", CLOCK_REALTIME}
};
#define NCLOCKS (sizeof(clocks) / sizeof(clocks[0]))
void bench_clock_gettime(clockid_t clkid)
{
unsigned long loop;
struct timespec tmpspec;
for (loop=0; loop<NLOOP; loop++) {
UNROLL( (void)clock_gettime(clkid, &tmpspec) );
}
}
#endif /* HAVE_CLOCK_GETTIME */
void bench_time(clockid_t clkid)
{
unsigned long loop;
for (loop=0; loop<NLOOP; loop++) {
UNROLL( (void)time(NULL) );
}
}
void bench_usleep(clockid_t clkid)
{
unsigned long loop;
for (loop=0; loop<NLOOP; loop++) {
UNROLL( (void)usleep(1000) );
}
}
void bench_gettimeofday(clockid_t clkid)
{
unsigned long loop;
struct timeval tmpval;
for (loop=0; loop<NLOOP; loop++) {
UNROLL( (void)gettimeofday(&tmpval, NULL) );
}
}
void bench_clock(clockid_t clkid)
{
unsigned long loop;
for (loop=0; loop<NLOOP; loop++) {
UNROLL( (void)clock() );
}
}
void benchmark(const char *name, void (*func) (clockid_t clkid), clockid_t clkid)
{
unsigned int run;
double dt, best;
#ifdef HAVE_CLOCK_GETTIME
struct timespec before, after;
#else
struct timeval before, after;
#endif
struct timeval tmpval;
best = -1.0;
for (run=0; run<NRUN; run++) {
#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_MONOTONIC, &before);
(*func) (clkid);
clock_gettime(CLOCK_MONOTONIC, &after);
dt = (after.tv_sec - before.tv_sec) * 1e9;
if (after.tv_nsec >= before.tv_nsec)
dt += (after.tv_nsec - before.tv_nsec);
else
dt -= (before.tv_nsec - after.tv_nsec);
#else
gettimeofday(&before, NULL);
(*func) (clkid);
gettimeofday(&after, NULL);
dt = (after.tv_sec - before.tv_sec) * 1e9;
if (after.tv_usec >= before.tv_usec)
dt += (after.tv_usec - before.tv_usec) * 1e3;
else
dt -= (before.tv_usec - after.tv_usec) * 1e3;
#endif
dt /= NLOOP;
dt /= NUNROLL;
if (best != -1.0) {
if (dt < best)
best = dt;
}
else
best = dt;
}
printf("%s: %.0f ns\n", name, best, NLOOP);
}
int main()
{
#ifdef HAVE_CLOCK_GETTIME
clockid_t clkid;
int i;
for (i=0; i<NCLOCKS; i++) {
benchmark(clocks[i].name, bench_clock_gettime, clocks[i].identifier);
}
#endif
benchmark("clock()", bench_clock, 0);
benchmark("gettimeofday()", bench_gettimeofday, 0);
benchmark("time()", bench_time, 0);
return 0;
}