-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark_AnonymousVisitor.cpp
162 lines (129 loc) · 3.61 KB
/
Benchmark_AnonymousVisitor.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
/******************************************************************************
Virtual Table OR The overhead of magic
======================================
Author: Inbal Levi
Description: This file contains test you can run in order to do VT
Benchmarking, is free for use, with open license.
NOTES: Try running with flags -O0, -O1, -O2, -O3, -Os
Try running with PHOTO_SIZE 100, 1000, 10000 etc.
(USE "ulimit -s unlimited" to increase stack size)
*******************************************************************************/
#include <iostream>
#include <ctime>
#include <variant>
using namespace std; // (shouldn't...)
#define PHOTO_SIZE 1000
void initPhoto(int photo[][PHOTO_SIZE])
{
int i,j;
for (i=0;i<PHOTO_SIZE;++i)
{
for (j=0;j<PHOTO_SIZE;++j)
{
photo[i][j]= (unsigned char) rand();
}
}
}
void printPhoto(int photo[][PHOTO_SIZE])
{
int i,j;
for (i=0;i<PHOTO_SIZE;++i)
{
for (j=0;j<PHOTO_SIZE;++j)
{
cout<<" %d "<< photo[i][j];
}
cout <<"\n";
}
}
class FilterBright{};
class FilterDark{};
using Pixel = int;
using var_t = std::variant<FilterBright, FilterDark>;
using pixel_t = std::variant<Pixel>;
template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
template <class DerivedFilter>
class BaseFilter
{
public:
constexpr void Activate(int *pixel)
{
static_cast<DerivedFilter*>(this)->ImplementFilter(pixel);
}
};
class FilterDerived : public BaseFilter<FilterDerived>
{
public:
constexpr void ImplementFilter(int *pixel)
{
//cout << "FilterA implementation" << endl;
*pixel-=1;
}
};
class BaseFilterVirtual
{
public:
virtual inline void Activate(int *pixel) const
{
cout << "BaseFilterVirtual Activate" <<endl;
}
virtual ~BaseFilterVirtual() = default;
};
class FilterV : public BaseFilterVirtual
{
public:
virtual inline void Activate(int *pixel) const override
{
//cout << "FilterV implementation" << endl;
*pixel-=1;
}
};
int main()
{
int photo[PHOTO_SIZE][PHOTO_SIZE];
initPhoto(photo);
// printPhoto(photo);
BaseFilterVirtual * pt_fv = new FilterV();
clock_t Vbegin = clock();
int vi,vj;
for (vi=0;vi<PHOTO_SIZE;++vi)
{
for (vj=0;vj<PHOTO_SIZE;++vj)
{
pt_fv->Activate(&photo[vi][vj]);
}
}
clock_t Vend = clock();
delete pt_fv;
cout <<"Vtable implementation: "<< Vend - Vbegin << endl;
FilterDerived fc;
FilterDerived * pt_fc = &fc;
clock_t Cbegin = clock();
int i,j;
for (i=0;i<PHOTO_SIZE;++i)
{
for (j=0;j<PHOTO_SIZE;++j)
{
pt_fc->Activate(&photo[i][j]);
}
}
clock_t Cend = clock();
cout <<"CRTP implementation: "<< Cend - Cbegin << endl;
pixel_t photoS[PHOTO_SIZE][PHOTO_SIZE]; // Recreating a photo as pixel_t type
var_t filter{FilterBright()};
int Si,Sj;
clock_t Sbegin = clock();
for (Si=0;Si<PHOTO_SIZE;++Si)
{
for (Sj=0;Sj<PHOTO_SIZE;++Sj)
{
std::visit (overload{
[](FilterBright f, Pixel& pixel) { pixel+=1; },
[](FilterDark f, Pixel& pixel) { pixel-=1; },
}, filter, photoS[Si][Sj]);
}
}
clock_t Send = clock();
cout <<"Visitor implementation: "<< Send - Sbegin << endl;
}