-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeouts.c
95 lines (89 loc) · 1.26 KB
/
timeouts.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
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
void
select0()
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
select(0, NULL, NULL, NULL, &tv);
}
void
select1()
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1;
select(0, NULL, NULL, NULL, &tv);
}
void
sleep0()
{
sleep(0);
}
void
sleep1()
{
sleep(1);
}
void
usleep0()
{
usleep(0);
}
void
usleep1()
{
usleep(1);
}
void
nanosleep0()
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 0;
nanosleep(&ts, NULL);
}
void
nanosleep1()
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1;
nanosleep(&ts, NULL);
}
#define E(x) { x, #x },
struct test { void(*f)(); char *n; };
struct test tests[] = {
E(select0)
E(select1)
E(sleep0)
E(sleep1)
E(usleep0)
E(usleep1)
E(nanosleep0)
E(nanosleep1)
{ NULL, NULL }
};
int
main(int ac, char **av)
{
(void)ac;
(void)av;
const struct test *p = tests;
while (p->f) {
printf("calling %s: ", p->n);
struct timeval start, end;
gettimeofday(&start, NULL);
p->f();
gettimeofday(&end, NULL);
long long diff = (end.tv_sec * 1000000 + end.tv_usec) -
(start.tv_sec * 1000000 + start.tv_usec) ;
printf("time %lld\n", diff);
p++;
}
return 0;
}