-
Notifications
You must be signed in to change notification settings - Fork 0
/
timed.h
170 lines (145 loc) · 5.33 KB
/
timed.h
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
#ifndef _timed_h
#define _timed_h
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
extern size_t* g_xmem_pmalloced_size;
struct ToddTime {
float utime;
float stime;
float TIMED_MS(struct timeval& r) { return r.tv_sec*1000 + r.tv_usec/1000.0; }
size_t xmem_malloced_size;
char const* prompt;
int output; // 1 means stdout, 2 means stderr
bool enable;
int& level() {
static int _level = 0;
return _level;
}
char* spaces() {
static char _spaces[] =
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
;
return _spaces;
}
ToddTime(char const* prompt, int output, bool enable)
: prompt(prompt)
, output(output)
, enable(enable && (output == 1 || output == 2)) {
if (enable) {
if (output == 0)
printf("%.*s~~Enter %s:\n", level(), spaces(), prompt);
struct rusage ru;
::getrusage(RUSAGE_SELF, &ru);
utime = TIMED_MS(ru.ru_utime);
stime = TIMED_MS(ru.ru_stime);
xmem_malloced_size = *g_xmem_pmalloced_size;
level() += 3;
}
else { // useless of this, only to disable coverity warning
utime = stime = 0;
xmem_malloced_size = 0;
}
}
~ToddTime() {
if (enable) {
level() -= 3;
struct rusage ru;
::getrusage(RUSAGE_SELF, &ru);
utime = TIMED_MS(ru.ru_utime) - utime;
stime = TIMED_MS(ru.ru_stime) - stime;
char* curr = (char*)::sbrk(0);
FILE* stream = output == 1 ? stdout : stderr;
::fprintf(stream,
"%.*s[%-20s] %10.5gms/user %10.5gms/sys %10u bytes/mem\n",
level(),
spaces(),
prompt,
utime,
stime,
(unsigned int)(*g_xmem_pmalloced_size-xmem_malloced_size)
);
}
}
};
struct ToddTimeClock {
char const* prompt;
int output; // 1 means stdout, 2 means stderr
bool enable;
clock_t ticks;
size_t xmem_malloced_size;
int& level() {
static int _level = 0;
return _level;
}
char* spaces() {
static char _spaces[] =
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
;
return _spaces;
}
ToddTimeClock(char const* prompt, int output, bool enable)
: prompt(prompt)
, output(output)
, enable(enable && (output == 1 || output == 2)) {
if (enable) {
if (output == 0)
printf("%.*s~~Enter %s:\n", level(), spaces(), prompt);
level() += 3;
xmem_malloced_size = *g_xmem_pmalloced_size;
ticks = clock();
}
}
~ToddTimeClock() {
if (enable) {
ticks = clock() - ticks;
level() -= 3;
FILE* stream = output == 1 ? stdout : stderr;
::fprintf(stream,
"%.*s[%-20s] %g ms/clock %u bytes/mem\n",
level(),
spaces(),
prompt,
double(ticks)*1000/CLOCKS_PER_SEC,
(unsigned int)(*g_xmem_pmalloced_size-xmem_malloced_size)
);
}
}
};
#if defined(__CYGWIN__)
#define TIMED_BLOCK_STDOUT(block, enable) ToddTimeClock toddtime_##block(#block, 1, enable);
#define TIMED_BLOCK_STDERR(block, enable) ToddTimeClock toddtime_##block(#block, 2, enable);
#else
#define TIMED_BLOCK_STDOUT(block, enable) ToddTime toddtime_##block(#block, 1, enable);
#define TIMED_BLOCK_STDERR(block, enable) ToddTime toddtime_##block(#block, 2, enable);
#endif
#endif