-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPolymorphismPerf.cpp
More file actions
61 lines (55 loc) · 1.67 KB
/
Copy pathPolymorphismPerf.cpp
File metadata and controls
61 lines (55 loc) · 1.67 KB
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
/*************************************************************************
> File Name: PolymorphismBenmark.cpp
> Author: Netcan
> Blog: https://netcan.github.io/
> Mail: netcan1996@gmail.com
> Created Time: 2021-02-02 21:44
************************************************************************/
#include <benchmark/benchmark.h>
#include <cmath>
#include <vector>
#include <memory>
#include <cstdlib>
#include "ShapeAdhoc.hpp"
#include "ShapeSubtype.hpp"
using namespace std;
constexpr size_t kMaxLen = 1<<24;
static void subtypePerf() {
using namespace Subtype;
vector<unique_ptr<Shape>> shapes;
shapes.reserve(kMaxLen);
for (size_t i = 0; i < kMaxLen; ++i) {
if (rand() % 100 > 50) {
shapes.emplace_back(make_unique<Rectangle>(rand() % 10, rand() % 10));
} else {
shapes.emplace_back(make_unique<Circle>(rand() % 10));
}
}
for (auto&& shape: shapes) {
benchmark::DoNotOptimize(shape->getArea());
benchmark::DoNotOptimize(shape->getPerimeter());
}
}
static void adhocPerf() {
using namespace Adhoc;
vector<Shape> shapes;
shapes.reserve(kMaxLen);
for (size_t i = 0; i < kMaxLen; ++i) {
if (rand() % 100 > 50) {
shapes.emplace_back(Rectangle{rand() % 10 * 1.0, rand() % 10 * 1.0});
} else {
shapes.emplace_back(Circle{rand() % 10 * 1.0});
}
}
for (auto&& shape: shapes) {
benchmark::DoNotOptimize(getArea(shape));
benchmark::DoNotOptimize(getPerimeter(shape));
}
}
int main(int argc, char** argv) {
for (int i = 0; i<10; ++i) {
subtypePerf();
adhocPerf();
}
return 0;
}