-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
96 lines (79 loc) · 1.94 KB
/
timer.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
#include <iostream>
#include <fstream>
#include <chrono>
#include <string>
#include <cstring>
#include <utility>
#include "Life.h"
using namespace std;
void usage() {
cout << "Usage: timer.exe PATTERN_FILENAME NUM_ITERATIONS [--stats]"
<< endl;
}
void print_info(Life &life) {
auto min_pos = life.min_pos();
auto max_pos = life.max_pos();
cout << "size\t" << life.size() << endl;
cout << "dims\t" << life.width() << " by " << life.height() << endl;
cout << "span\t"
<< "(" << min_pos.first << "," << min_pos.second << ")" << " to "
<< "(" << max_pos.first << "," << max_pos.second << ")" << endl;
cout << endl;
}
int main(int argc, char **argv) {
// parse arguments
if (argc < 3 || argc > 4) {
usage();
return 1;
}
bool print_info_flag = false;
if (argc == 4) {
if (strcmp(argv[3], "--stats") == 0) {
print_info_flag = true;
}
else {
usage();
return 1;
}
}
int iter;
try {
iter = stoi(argv[2]);
}
catch (invalid_argument &e) {
cout << "Error: converting " << argv[2] << " to an int: "
<< e.what() << endl;
return 1;
}
if (iter < 1) {
cout << "Error: number of iterations must be a positive integer" << endl;
return 1;
}
ifstream fin(argv[1]);
if (!fin.is_open()) {
cout << "Error: could not open " << argv[1] << endl;
return 1;
}
// conduct test
Life life;
life.add_rle(fin, 0, 0);
if (print_info_flag) {
print_info(life);
}
auto t1 = chrono::high_resolution_clock::now();
for (int i = 0; i < iter; ++i) {
life.progress();
}
auto t2 = chrono::high_resolution_clock::now();
auto diff = chrono::duration_cast<chrono::milliseconds>(t2 - t1).count();
auto m = diff / 60000;
auto s = (diff % 60000) / 1000;
auto ms = diff % 1000;
if (print_info_flag) {
print_info(life);
}
cout << "time\t" << m << "m" << s << ".";
cout.fill('0');
cout.width(3);
cout << ms << "s" << endl;
}