-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
187 lines (148 loc) · 5.28 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <cstdlib>
#include <cstring>
#include <array>
#include <vector>
#include <chrono>
#include <iostream>
#include <memory>
#include <numeric>
template<std::size_t N>
void fillData (std::array<double, N>& data)
{
for (auto& d : data)
d = (rand() / (double) RAND_MAX);
}
//==============================================================================
struct ValueBase
{
virtual ~ValueBase() {}
using DataRange = std::pair<const double*, std::size_t>;
virtual DataRange getData() const = 0;
double count() const
{
auto data = getData();
return std::accumulate (data.first, data.first + data.second, 0.0);
}
};
template<std::size_t N>
struct ValueDerived : public ValueBase
{
ValueDerived() { fillData (data); }
DataRange getData() const override { return { data.data(), data.size() }; }
std::array<double, N> data;
};
/** Use some 'real-world-like' struct sizes. */
static constexpr std::size_t struct1 = 81;
static constexpr std::size_t struct2 = 162;
static constexpr std::size_t struct3 = 243;
using ValueType0 = ValueDerived<struct1>;
using ValueType1 = ValueDerived<struct2>;
using ValueType2 = ValueDerived<struct3>;
//==============================================================================
/** My dodgy variant - no std::variant in my version of Xcode, plus it's nice
to have all the machinary visible, because I'm interested in the perf of
the memory access patterns, not std::variant specifically.
Obviously this is a super-dangerous footgun, not how you'd do this in reality!
*/
struct TaggedUnion
{
TaggedUnion (ValueType0 t) { value.t0 = t; tag = Tag::type0; }
TaggedUnion (ValueType1 t) { value.t1 = t; tag = Tag::type1; }
TaggedUnion (ValueType2 t) { value.t2 = t; tag = Tag::type2; }
double count() const
{
switch (tag)
{
case Tag::type0:
return std::accumulate (value.t0.data.begin(), value.t0.data.end(), 0.0);
case Tag::type1:
return std::accumulate (value.t1.data.begin(), value.t1.data.end(), 0.0);
case Tag::type2:
return std::accumulate (value.t2.data.begin(), value.t2.data.end(), 0.0);
default: return 0.0;
}
}
enum class Tag
{
type0,
type1,
type2
};
union Value
{
Value() { new (&t0) ValueType0(); }
Value (const Value& o) { std::memcpy (this, &o, sizeof (Value)); }
~Value() { }
ValueType0 t0;
ValueType1 t1;
ValueType2 t2;
};
Value value;
Tag tag;
};
//==============================================================================
int main (int argc, char* argv[])
{
std::vector<std::unique_ptr<ValueBase>> ptrVec;
std::vector<std::unique_ptr<ValueBase>> heapFragmenter;
std::vector<TaggedUnion> unionVec;
constexpr std::size_t vecLength = 1000000;
constexpr int runThroughs = 5;
ptrVec.reserve (vecLength);
heapFragmenter.reserve (vecLength / 2);
unionVec.reserve (vecLength);
for (std::size_t i = 0; i < vecLength; ++i)
{
const auto rand1 = rand() % 3;
const auto rand2 = rand() % 2;
switch (rand1)
{
case 0:
ptrVec.emplace_back (std::make_unique<ValueType0>());
unionVec.push_back (ValueType0());
break;
case 1:
ptrVec.emplace_back (std::make_unique<ValueType1>());
unionVec.push_back (ValueType1());
break;
case 2:
ptrVec.emplace_back (std::make_unique<ValueType2>());
unionVec.push_back (ValueType2());
break;
default:
break;
}
if (rand2 == 0)
heapFragmenter.emplace_back (std::make_unique<ValueType2>());
}
double ptrThrowawayResult = 0.0;
double unionThrowawayResult = 0.0;
double ptrTimeUs = 0.0;
double unionTimeUs = 0.0;
/** VECTOR OF POINTERS */
for (int i = 0; i < runThroughs; ++i)
{
const auto start = std::chrono::high_resolution_clock::now();
for (auto& ptr : ptrVec)
ptrThrowawayResult += ptr->count();
const auto end = std::chrono::high_resolution_clock::now();
const auto elapsed = std::chrono::duration<double, std::micro>(end - start).count();
ptrTimeUs = (i == 0) ? elapsed : (ptrTimeUs + elapsed) * 0.5;
}
/** VECTOR OF TAGGED UNIONS */
for (int i = 0; i < runThroughs; ++i)
{
const auto start = std::chrono::high_resolution_clock::now();
for (auto& un : unionVec)
unionThrowawayResult += un.count();
const auto end = std::chrono::high_resolution_clock::now();
const auto elapsed = std::chrono::duration<double, std::micro>(end - start).count();
unionTimeUs = (i == 0) ? elapsed : (unionTimeUs + elapsed) * 0.5;
}
std::cout << vecLength << " iterations. \n"
<< "struct sizes: " << struct1 << ", "<< struct2 << ", " << struct3 << " (doubles)\n-------- \n"
<< "vec of pointers took: " << ptrTimeUs << "us \n"
<< "vec of unions took: " << unionTimeUs << "us \n";
std::cout << "ptrs result: " << ptrThrowawayResult << ", union result: " << unionThrowawayResult << "\n";
return 0;
}