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
#include <protoTimer.h>
#include <stdio.h>
#include <stdlib.h> // for rand()
// This test program benchmarks the ProtoTimer insertion cost
// as increasing numbers of timers are activated.
// These functions measure CPU time used
#ifdef WIN32
#include <Windows.h>
double get_cpu_time()
{
FILETIME a,b,c,d;
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)(d.dwLowDateTime |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
}else{
// Handle error
return 0;
}
}
#else // UNIX
#include <time.h>
#include <sys/time.h>
double get_cpu_time()
{
return (double)clock() / CLOCKS_PER_SEC;
}
#endif // if/else WIN32/UNIX
double UniformRand(double min, double max)
{
double range = max - min;
return (((((double)rand()) * range) / ((double)RAND_MAX)) + min);
}
class TestTimerMgr : public ProtoTimerMgr
{
public:
bool UpdateSystemTimer(ProtoTimer::Command command,
double delay) {return true;}
void OnTimeout(ProtoTimer& theTimer)
{
//fprintf(stderr, "timer timeout interval: %lf\n", theTimer.GetInterval());
count++;
}
unsigned int count;
};
int main(int argc, char* argv[])
{
TestTimerMgr mgr;
mgr.count = 0;
fprintf(stderr, "sizeof(ProtoTimer) = %lu bytes\n", (unsigned long)sizeof(ProtoTimer));
ProtoTime t1, t2;
/*for (int size = 2; size < 5000; size *= 1.5)
{
double total = 0.0;
ProtoTimer* array = new ProtoTimer[size];
int trials = 5000;
for (int k = 0; k < trials; k++)
{
t1.GetCurrentTime();
for (int i = 0; i < size; i++)
{
double interval = UniformRand(0.0, 8.0);
array[i].SetInterval(interval);
mgr.ActivateTimer(array[i]);
}
t2.GetCurrentTime();
total += ProtoTime::Delta(t2, t1) / (double)size;
for (int i = 0; i < size; i++)
array[i].Deactivate();
}
delete[] array;
double ave = total / (double)trials;
//printf("size:%d items average time: %lf seconds\n", size, ave);
printf("%d, %e\n", size/2, ave);
}
*/
// Run a bunch of very short timouts and benchmark
int trials = 10;
//int size = 10000;
for (int size = 2; size < 100000; size *= 1.5)
{
double total = 0.0;
for (int k = 0; k < trials; k++)
{
ProtoTimer* array = new ProtoTimer[size];
for (int i = 0; i < size; i++)
{
double interval = UniformRand(10.0e-06, 100.0e-06);
array[i].SetInterval(interval);
array[i].SetRepeat(-1);
array[i].SetListener(&mgr, &TestTimerMgr::OnTimeout);
mgr.ActivateTimer(array[i]);
}
t1.GetCurrentTime();
double elapsed = 0.0;
double c1 = get_cpu_time();
while (elapsed < 0.5)
{
double wait = mgr.GetTimeRemaining();
struct timespec ts;
if (wait < 0)
{
ts.tv_sec = 1;
ts.tv_nsec = 0;
}
else
{
ts.tv_sec = (time_t)wait;
ts.tv_nsec = (wait - (time_t)wait)*1.0e+09;
}
//fprintf(stderr, "waiting %lf sec ...\n", wait);
nanosleep(&ts, NULL);
mgr.OnSystemTimeout();
t2.GetCurrentTime();
elapsed = ProtoTime::Delta(t2, t1);
}
double c2 = get_cpu_time();
total += c2 - c1;
for (int i = 0; i < size; i++)
array[i].Deactivate();
delete[] array;
}
printf("%d, %lf\n", size, total / (double)trials);
fprintf(stderr, "%d, %lf\n", size, total / (double)trials);
}
} // end main()