-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiotest.cpp
442 lines (388 loc) · 8.19 KB
/
iotest.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <exception>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <string>
using namespace std;
//#define USE_SIGN // Define this to have signed custom int I/O
const int limit = 10000000; // How many objects to output
const int retries = 3; // How many times to run each test
// Turn on iostreams optimizations
inline void init()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
//
// Custom int write and read functions
//
// FIXME: Can't read or write INT_MIN
// TODO: Can these functions be further sped up?
//
inline void write_int(int x)
{
#ifdef USE_SIGN
if (x < 0)
{
putchar('-');
x = -x;
}
#endif
char buf[10], *p = buf;
do
{
*p++ = '0' + x % 10;
x /= 10;
}
while (x);
do
{
putchar(*--p);
}
while (p > buf);
}
inline int read_int()
{
char c;
while (c = getchar(), c <= ' ');
#ifdef USE_SIGN
bool sign = c == '-';
if (sign)
{
c = getchar();
}
#endif
int res = c - '0';
while (c = getchar(), c >= '0' && c <= '9')
{
res = res * 10 + (c - '0');
}
// One character is gobbled here
#ifdef USE_SIGN
return sign ? -res : res;
#else
return res;
#endif
}
//
// Begin tests
//
void int_printf()
{
for (int i = 0; i < limit; i++)
{
printf("%d\n", i);
}
}
void int_cout()
{
init();
for (int i = 0; i < limit; i++)
{
cout << i << '\n';
}
}
void int_write_int()
{
for (int i = 0; i < limit; i++)
{
write_int(i);
putchar('\n');
}
}
void int_scanf()
{
for (int i = 0; i < limit; i++)
{
int x;
scanf("%d", &x);
}
}
void int_cin()
{
init();
for (int i = 0; i < limit; i++)
{
int x;
cin >> x;
}
}
void int_read_int()
{
for (int i = 0; i < limit; i++)
{
read_int();
}
}
void double_printf()
{
for (int i = 0; i < limit; i++)
{
printf("%.6f\n", static_cast<double>(i));
}
}
void double_cout()
{
init();
cout.setf(ios::fixed);
cout.precision(6);
for (int i = 0; i < limit; i++)
{
cout << static_cast<double>(i) << '\n';
}
}
void double_scanf()
{
for (int i = 0; i < limit; i++)
{
double x;
scanf("%lf", &x);
}
}
void double_cin()
{
init();
for (int i = 0; i < limit; i++)
{
double x;
cin >> x;
}
}
void char_putchar()
{
for (int i = 0; i < limit * 10; i++)
{
putchar(' ' + i % 64);
}
putchar('\n');
}
void char_cout()
{
init();
for (int i = 0; i < limit * 10; i++)
{
cout.put(' ' + i % 64);
}
cout.put('\n');
}
void char_getchar()
{
for (int i = 0; i < limit * 10; i++)
{
getchar();
}
}
void char_cin()
{
init();
for (int i = 0; i < limit * 10; i++)
{
cin.get();
}
}
const char s[] = "abcdefghijklmnop";
const string str(s);
char buf[sizeof(s)];
void str_printf()
{
for (int i = 0; i < limit; i++)
{
printf("%s\n", s);
}
}
void str_puts()
{
for (int i = 0; i < limit; i++)
{
puts(s);
}
}
void str_cout()
{
init();
for (int i = 0; i < limit; i++)
{
cout << s << '\n';
}
}
void string_cout()
{
init();
for (int i = 0; i < limit; i++)
{
cout << str << '\n';
}
}
void str_scanf()
{
for (int i = 0; i < limit; i++)
{
scanf("%s", buf);
}
}
void str_gets()
{
for (int i = 0; i < limit; i++)
{
gets(buf);
}
}
void str_cin()
{
init();
for (int i = 0; i < limit; i++)
{
cin.getline(buf, sizeof(buf));
}
}
void string_getline()
{
init();
for (int i = 0; i < limit; i++)
{
//
// We can most certainly speed up this loop by moving x outside. But
// this would differ with what we are trying to emulate: reading many
// strings into a string array, where each element is initialized anew.
//
string x;
//
// In most problems we know the maximum possible length of a string,
// thus we can tell x about it.
//
x.reserve(sizeof(s));
getline(cin, x);
}
}
//
// End tests
//
enum io_dir {
IN,
OUT
};
struct test_entry {
string name; // Test name
void (*func)(); // Test function
io_dir dir; // I/O direction
};
// List of tests
const test_entry entries[] = {
{ "int, printf", int_printf, OUT },
{ "int, cout", int_cout, OUT },
{ "int, custom/out", int_write_int, OUT },
{ "int, scanf", int_scanf, IN },
{ "int, cin", int_cin, IN },
{ "int, custom/in", int_read_int, IN },
{ "double, printf", double_printf, OUT },
{ "double, cout", double_cout, OUT },
{ "double, scanf", double_scanf, IN },
{ "double, cin", double_cin, IN },
{ "char, putchar", char_putchar, OUT },
{ "char, cout", char_cout, OUT },
{ "char, getchar", char_getchar, IN },
{ "char, cin", char_cin, IN },
{ "char *, printf", str_printf, OUT },
{ "char *, puts", str_puts, OUT },
{ "char *, cout", str_cout, OUT },
{ "string, cout", string_cout, OUT },
{ "char *, scanf", str_scanf, IN },
{ "char *, gets", str_gets, IN },
{ "char *, cin", str_cin, IN },
{ "string, getline", string_getline, IN }
};
int main(int argc, char *argv[])
try
{
if (argc == 0)
{
// Can't run itself
throw runtime_error("program name not available");
}
else if (argc == 1)
{
//
// This is the main process
//
// Get a temporary file name
const char *fname = tmpnam(NULL);
if (!fname)
{
throw runtime_error("tmpnam() failed");
}
// Run all tests
for (size_t i = 0; i < sizeof(entries)/sizeof(entries[0]); i++)
{
cerr << left << setw(15) << entries[i].name << " " << flush;
// Prepare the command
ostringstream cmd;
cmd << argv[0] << " " << i
<< (entries[i].dir == OUT ? " > " : " < ") << fname;
// Repeat the command 'retries' times
for (int j = 0; j < retries; j++)
{
// If this is a write test, clean up leftovers
if (entries[i].dir == OUT && ifstream(fname))
{
if (remove(fname) != 0)
{
throw runtime_error("remove() failed");
}
}
// Run the command
if (system(cmd.str().c_str()) != 0)
{
return EXIT_FAILURE;
}
}
cerr << endl;
}
// Final cleanup
if (remove(fname) != 0)
{
throw runtime_error("remove() failed");
}
}
else
{
//
// This is the child process
//
// Get the test number
istringstream param(argv[1]);
int i;
param >> i;
// Run and time the function
clock_t start = clock();
entries[i].func();
if (entries[i].dir == OUT)
{
cout << flush;
}
clock_t end = clock();
//
// Check for errors. Unfortunately, this check doesn't always work
// with stdio. However, placing error checks into the test functions
// would incur a performance penalty, so we leave it as it is.
//
// I hope calling ferror() is fine even with sync_with_stdio(false).
//
if (ferror(stdin) || !cin.good() || ferror(stdout) || !cout.good())
{
throw runtime_error("input/output error");
}
// Output result
cerr << fixed << setw(7) << setprecision(2) << right
<< static_cast<double>(end - start) / CLOCKS_PER_SEC << flush;
}
}
catch (const exception& e)
{
cerr << "error: " << e.what() << endl;
return EXIT_FAILURE;
}