-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark_SimpleVisitor.cpp
153 lines (126 loc) · 3.35 KB
/
Benchmark_SimpleVisitor.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
/******************************************************************************
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>
#define PHOTO_SIZE 100
using namespace std; // shouldn't
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";
}
}
struct FilterBright { };
struct FilterDark { };
struct VisitActivate
{
constexpr void operator()(FilterBright& filter, int& pixel) const { pixel+=1; }
constexpr void operator()(FilterDark&, int& pixel) const { pixel+=1; }
};
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();
int vi,vj;
clock_t Vbegin = clock();
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;
BaseFilter<FilterDerived> fc;
BaseFilter<FilterDerived> * pt_fc = &fc;
int i,j;
clock_t Cbegin = clock();
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;
std::variant<int> photoS[PHOTO_SIZE][PHOTO_SIZE];
std::variant<FilterBright, FilterDark> MyFilter { FilterBright() };
int Si,Sj;
clock_t Sbegin = clock();
for (Si=0;Si<PHOTO_SIZE;++Si)
{
for (Sj=0;Sj<PHOTO_SIZE;++Sj)
{
std::visit(VisitActivate(), MyFilter, photoS[Si][Sj]);
}
}
clock_t Send = clock();
cout <<"Visitor implementation: "<< Send - Sbegin << endl;
}