-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvlTreePerfTest.cpp
More file actions
221 lines (185 loc) · 5 KB
/
AvlTreePerfTest.cpp
File metadata and controls
221 lines (185 loc) · 5 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <AvlTree.hpp>
#include <chrono>
#include <iostream>
#include <set>
// Helpers
static size_t SideEffect = 0;
static const size_t COUNT = 4 * 1024 * 1024;
static char SimpleBuffer[COUNT * 64];
static size_t SimpleBufferPos = 0;
static void* simpleAlloc(size_t aSize)
{
assert(SimpleBufferPos + aSize <= sizeof(SimpleBuffer));
void* sRes = SimpleBuffer + SimpleBufferPos;
SimpleBufferPos += aSize;
return sRes;
}
template <class T>
static T* simpleAlloc()
{
return static_cast<T*>(simpleAlloc(sizeof(T)));
}
template <class T>
struct StdAllocator
{
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
StdAllocator() = default;
template <class U> constexpr StdAllocator(const StdAllocator<U>&) noexcept {}
template <class U> struct rebind { typedef StdAllocator<U> other; };
T* allocate(std::size_t n) { assert(n == 1); (void)n; return simpleAlloc<T>(); }
void deallocate(T*, std::size_t n) noexcept { assert(n == 1); (void)n; }
void destroy(T* p) { p->~T(); }
};
static size_t simpleReset()
{
size_t sRes = SimpleBufferPos;
SimpleBufferPos = 0;
return sRes;
}
static void checkpoint(const char* aText, size_t aOpCount)
{
using namespace std::chrono;
high_resolution_clock::time_point now = high_resolution_clock::now();
static high_resolution_clock::time_point was;
duration<double> time_span = duration_cast<duration<double>>(now - was);
if (0 != aOpCount)
{
double Mrps = aOpCount / 1000000. / time_span.count();
std::cout << aText << ": " << Mrps << " Mrps" << std::endl;
}
was = now;
}
// Avl tree with size_t key
struct Test
{
size_t m_Value;
Avl::Node m_Node;
bool operator<(const Test& a) const { return m_Value < a.m_Value; }
bool operator<(size_t a) const { return m_Value < a; }
friend bool operator<(size_t a, const Test& b) { return a < b.m_Value; }
};
using Tree_t = Avl::Tree<Test, &Test::m_Node>;
static void alv_test()
{
Tree_t sTree;
checkpoint("", 0);
for (size_t i = 0; i < COUNT; i++)
{
Test* t = simpleAlloc<Test>();
t->m_Value = i;
sTree.insert(*t);
}
checkpoint("AVL insert", COUNT);
for (size_t i = 0; i < COUNT; i++)
{
SideEffect ^= sTree.find(i)->m_Value;
}
checkpoint("AVL find", COUNT);
for (Test& t : sTree)
{
SideEffect ^= t.m_Value;
}
checkpoint("AVL iteration", COUNT);
for (size_t i = 0; i < COUNT; i++)
{
sTree.erase(*sTree.begin());
}
checkpoint("AVL erase", COUNT);
std::cout << "AVL memory: " << simpleReset() / 1024 << "kB" << std::endl;
srand(0);
for (size_t i = 0; i < COUNT; i++)
{
Test* t = simpleAlloc<Test>();
t->m_Value = rand();
sTree.insert(*t);
}
checkpoint("AVL rand insert", COUNT);
srand(0);
for (size_t i = 0; i < COUNT; i++)
{
size_t val = rand();
SideEffect ^= sTree.find(val)->m_Value;
}
checkpoint("AVL rand find", COUNT);
for (Test& t : sTree)
{
SideEffect ^= t.m_Value;
}
checkpoint("AVL iteration", COUNT);
srand(0);
for (size_t i = 0; i < COUNT; i++)
{
size_t val = rand();
Tree_t::iterator itr = sTree.find(val);
if (itr != sTree.end())
sTree.erase(*itr);
}
checkpoint("AVL rand erase", COUNT);
std::cout << "AVL memory: " << simpleReset() / 1024 << "kB" << std::endl;
}
// Set size_t
using Set_t = std::set<size_t, std::less<size_t>, StdAllocator<size_t>>;
static void set_test()
{
Set_t sSet;
checkpoint("", 0);
for (size_t i = 0; i < COUNT; i++)
{
sSet.insert(i);
}
checkpoint("Set insert", COUNT);
for (size_t i = 0; i < COUNT; i++)
{
SideEffect ^= *sSet.find(i);
}
checkpoint("Set find", COUNT);
for (size_t i : sSet)
{
SideEffect ^= i;
}
checkpoint("Set iteration", COUNT);
for (size_t i = 0; i < COUNT; i++)
{
sSet.erase(*sSet.begin());
}
checkpoint("Set erase", COUNT);
std::cout << "Set memory: " << simpleReset() / 1024 << "kB" << std::endl;
srand(0);
for (size_t i = 0; i < COUNT; i++)
{
sSet.insert(rand());
}
checkpoint("Set rand insert", COUNT);
srand(0);
for (size_t i = 0; i < COUNT; i++)
{
size_t val = rand();
SideEffect ^= *sSet.find(val);
}
checkpoint("Set rand find", COUNT);
for (size_t i : sSet)
{
SideEffect ^= i;
}
checkpoint("Set iteration", COUNT);
srand(0);
for (size_t i = 0; i < COUNT; i++)
{
size_t val = rand();
sSet.erase(val);
}
checkpoint("Set rand erase", COUNT);
std::cout << "Set memory: " << simpleReset() / 1024 << "kB" << std::endl;
}
int main()
{
alv_test();
set_test();
std::cout << "Side effect (ignore it): " << SideEffect << std::endl;
}