-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
88 lines (75 loc) · 1.96 KB
/
main.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
#include <chrono>
#include <iostream>
#include <string>
#include <vector>
// Test Case base class
class TestCase {
public:
virtual void run() = 0;
virtual std::string name() = 0;
};
// Benchmark Suite class
class Benchmark {
public:
void add_case(TestCase* tc) {
cases.push_back(tc);
}
void run() {
for (auto tc : cases) {
std::cout << "Running " << tc->name() << "...\n";
auto start_time = std::chrono::high_resolution_clock::now();
tc->run();
auto end_time = std::chrono::high_resolution_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
std::cout << tc->name() << " elapsed time: " << elapsed_time << " us\n\n";
}
}
private:
std::vector<TestCase*> cases;
};
// Timer class
class Timer {
public:
Timer() {
reset();
}
void reset() {
start_time = std::chrono::high_resolution_clock::now();
}
int elapsed_us() {
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
}
private:
std::chrono::high_resolution_clock::time_point start_time;
};
// Reporter class
class Reporter {
public:
void report(const std::string& name, int elapsed_time) {
std::cout << name << " elapsed time: " << elapsed_time << " us\n";
}
};
// Example test case
class ExampleTestCase : public TestCase {
public:
void run() override {
Timer timer;
// do something to be benchmarked
for (int i = 0; i < 1000000; i++) {
int a = 1 + 1;
}
reporter.report(name(), timer.elapsed_us());
}
std::string name() override {
return "Example Test Case";
}
private:
Reporter reporter;
};
int main() {
Benchmark bench;
bench.add_case(new ExampleTestCase());
bench.run();
return 0;
}