-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-scaling.cpp
620 lines (502 loc) · 16.8 KB
/
test-scaling.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include "Map.hpp"
#include <chrono>
#include <random>
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
#include <assert.h>
#include <map>
#include <initializer_list>
#include <set>
//Enables iteration test on a map larger than the memory available to the remote cluster
//WARNING: This will be VERY slow.
#define DO_BIG_ITERATION_TEST 0
namespace cs440 {
template <typename K, typename V>
class StdMapWrapper {
private:
using base_map = std::map<K, V>;
public:
typedef typename base_map::iterator Iterator;
typedef typename base_map::const_iterator ConstIterator;
typedef typename base_map::reverse_iterator ReverseIterator;
typedef typename base_map::const_reverse_iterator ConstReverseIterator;
typedef typename base_map::value_type value_type;
typedef typename base_map::mapped_type mapped_type;
typedef typename base_map::key_type key_type;
StdMapWrapper() {}
StdMapWrapper(std::initializer_list<std::pair<K,V>> il) {
for(auto x : il) {
m_map.insert(x);
}
}
StdMapWrapper(StdMapWrapper &&other)
: m_map(std::move(other.m_map))
{}
StdMapWrapper(const StdMapWrapper &other)
: m_map(other.m_map)
{}
StdMapWrapper &operator=(const StdMapWrapper &other) {
if(this != &other) {
StdMapWrapper tmp(other);
std::swap(m_map, tmp.m_map);
}
return *this;
}
StdMapWrapper &operator=(const StdMapWrapper &&other) {
StdMapWrapper tmp(other);
std::swap(tmp.m_map, m_map);
return *this;
}
///////// Iterators
Iterator begin() {
return m_map.begin();
}
ConstIterator begin() const {
return m_map.begin();
}
ConstIterator cbegin() const {
return m_map.begin();
}
ReverseIterator rbegin() {
return m_map.rbegin();
}
/*
ConstReverseIterator rbegin() const {
return m_map.rbegin();
}
ConstReverseIterator crbegin() const {
return m_map.crbegin();
}
*/
Iterator end() {
return m_map.end();
}
ConstIterator end() const {
return m_map.end();
}
ConstIterator cend() const {
return m_map.cend();
}
ReverseIterator rend() {
return m_map.rend();
}
/*
ConstReverseIterator rend() const {
return m_map.rend();
}
ConstReverseIterator crend() const {
return m_map.crend();
}
*/
///////// Capacity
size_t size() const {
return m_map.size();
}
size_t max_size() const {
return m_map.max_size();
}
bool empty() const {
return m_map.empty();
}
///////// Modifiers
Iterator insert(const value_type &value) {
return m_map.insert(value).first;
}
Iterator insert(value_type &&value) {
return m_map.insert(std::move(value)).first;
}
void erase(const K &k) {
m_map.erase(k);
}
void erase(Iterator it) {
m_map.erase(it);
}
///////// Lookup
V &at(const K &k) {
return m_map.at(k);
}
const V &at(const K &k) const {
return m_map.at(k);
}
Iterator find(const K &k) {
return m_map.find(k);
}
ConstIterator find(const K &k) const {
return m_map.find(k);
}
V &operator[](const K &k) {
return m_map[k];
}
private:
base_map m_map;
template<typename A, typename B>
friend
bool operator==(const StdMapWrapper<A,B>&, const StdMapWrapper<A,B>&);
template<typename A, typename B>
friend bool operator!=(const StdMapWrapper<A,B>&, const StdMapWrapper<A,B>&);
template<typename A, typename B>
friend bool operator<=(const StdMapWrapper<A,B>&, const StdMapWrapper<A,B>&);
template<typename A, typename B>
friend bool operator<(const StdMapWrapper<A,B>&, const StdMapWrapper<A,B>&);
template<typename A, typename B>
friend bool operator>=(const StdMapWrapper<A,B>&, const StdMapWrapper<A,B>&);
template<typename A, typename B>
friend bool operator>(const StdMapWrapper<A,B>&, const StdMapWrapper<A,B>&);
};
template<typename K, typename T>
bool operator==(const StdMapWrapper<K,T> &a, const StdMapWrapper<K,T> &b) {
return a.m_map == b.m_map;
}
template<typename K, typename T>
bool operator!=(const StdMapWrapper<K,T> &a, const StdMapWrapper<K,T> &b) {
return a.m_map != b.m_map;
}
template<typename K, typename T>
bool operator<=(const StdMapWrapper<K,T> &a, const StdMapWrapper<K,T> &b) {
return a.m_map <= b.m_map;
}
template<typename K, typename T>
bool operator<(const StdMapWrapper<K,T> &a, const StdMapWrapper<K,T> &b) {
return a.m_map < b.m_map;
}
template<typename K, typename T>
bool operator>=(const StdMapWrapper<K,T> &a, const StdMapWrapper<K,T> &b) {
return a.m_map >= b.m_map;
}
template<typename K, typename T>
bool operator>(const StdMapWrapper<K,T> &a, const StdMapWrapper<K,T> &b) {
return a.m_map > b.m_map;
}
}
using Milli = std::chrono::duration<double, std::ratio<1,1000>>;
using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
void dispTestName(const char *testName, const char *typeName) {
std::cout << std::endl << std::endl << "************************************" << std::endl;
std::cout << "\t" << testName << " for " << typeName << "\t" << std::endl;
std::cout << "************************************" << std::endl << std::endl;
}
template <typename T>
T ascendingInsert(int count, bool print = true) {
using namespace std::chrono;
TimePoint start, end;
start = system_clock::now();
T map;
for(int i = 0; i < count; i++) {
map.insert(std::pair<int, int>(i,i));
}
end = system_clock::now();
Milli elapsed = end - start;
if(print)
std::cout << "Inserting " << count << " elements in aescending order took " << elapsed.count() << " milliseconds" << std::endl;
return map;
}
template <typename T>
T descendingInsert(int count, bool print = true) {
using namespace std::chrono;
TimePoint start, end;
start = system_clock::now();
T map;
for(int i = count; i > 0; i--) {
map.insert(std::pair<int, int>(i,i));
}
end = system_clock::now();
Milli elapsed = end - start;
if(print)
std::cout << "Inserting " << count << " elements in descending order took " << elapsed.count() << " milliseconds" << std::endl;
return map;
}
template <typename T>
void deleteTest() {
using namespace std::chrono;
TimePoint start, end;
T m1 = ascendingInsert<T>(10000, false);
T m2 = ascendingInsert<T>(100000, false);
T m3 = ascendingInsert<T>(1000000, false);
T m4 = ascendingInsert<T>(10000000, false);
std::set<int> toDelete;
for(int i = 0; i < 10000; i++) {
toDelete.insert(i);
}
start = system_clock::now();
for(const int e : toDelete)
m1.erase(e);
end = system_clock::now();
Milli elapsed1 = end - start;
std::cout << "deleting 10000 elements from a map of size 10000 took " << elapsed1.count() << " milliseconds" << std::endl;
{
toDelete.clear();
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,99999);
while(toDelete.size() < 10000) {
toDelete.insert(distribution(generator));
}
}
start = system_clock::now();
for(const int e : toDelete)
m2.erase(e);
end = system_clock::now();
Milli elapsed2 = end - start;
std::cout << "deleting 10000 elements from a map of size 100000 took " << elapsed2.count() << " milliseconds" << std::endl;
{
toDelete.clear();
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,999999);
while(toDelete.size() < 10000) {
toDelete.insert(distribution(generator));
}
}
start = system_clock::now();
for(const int e : toDelete)
m3.erase(e);
end = system_clock::now();
Milli elapsed3 = end - start;
std::cout << "deleting 10000 elements from a map of size 1000000 took " << elapsed3.count() << " milliseconds" << std::endl;
{
toDelete.clear();
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,9999999);
while(toDelete.size() < 10000) {
toDelete.insert(distribution(generator));
}
}
start = system_clock::now();
for(const int e : toDelete)
m4.erase(e);
end = system_clock::now();
Milli elapsed4 = end - start;
std::cout << "deleting 10000 elements from a map of size 10000000 took " << elapsed4.count() << " milliseconds" << std::endl;
}
template <typename T>
void findTest() {
using namespace std::chrono;
TimePoint start, end;
T m1 = ascendingInsert<T>(10000, false);
T m2 = ascendingInsert<T>(100000, false);
T m3 = ascendingInsert<T>(1000000, false);
T m4 = ascendingInsert<T>(10000000, false);
T m11;
T m22;
T m33;
T m44;
std::vector<int> toFind;
for(int i = 0; i < 10000; i++) {
toFind.push_back(i);
}
start = system_clock::now();
for(const int e : toFind) {
auto it = m1.find(e);
m11.insert(*it);
}
end = system_clock::now();
Milli elapsed1 = end - start;
std::cout << "Finding 10000 elements from a map of size " << m1.size() << " took " << elapsed1.count() << " milliseconds" << std::endl;
{
toFind.clear();
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,99999);
while(toFind.size() < 10000) {
toFind.push_back(distribution(generator));
}
}
start = system_clock::now();
for(const int e : toFind) {
auto it = m2.find(e);
m22.insert(*it);
}
end = system_clock::now();
Milli elapsed2 = end - start;
std::cout << "Finding 10000 elements from a map of size " << m2.size() << " took " << elapsed2.count() << " milliseconds" << std::endl;
{
toFind.clear();
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,999999);
while(toFind.size() < 10000) {
toFind.push_back(distribution(generator));
}
}
start = system_clock::now();
for(const int e : toFind) {
auto it = m3.find(e);
m33.insert(*it);
}
end = system_clock::now();
Milli elapsed3 = end - start;
std::cout << "Finding 10000 elements from a map of size " << m3.size() << " took " << elapsed3.count() << " milliseconds" << std::endl;
{
toFind.clear();
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,9999999);
while(toFind.size() < 10000) {
toFind.push_back(distribution(generator));
}
}
start = system_clock::now();
for(const int e : toFind) {
auto it = m4.find(e);
m44.insert(*it);
}
end = system_clock::now();
Milli elapsed4 = end - start;
std::cout << "Finding 10000 elements from a map of size " << m4.size() << " took " << elapsed4.count() << " milliseconds" << std::endl;
}
template <typename T>
void iterationTest(int count) {
using namespace std::chrono;
T m = ascendingInsert<T>(count,false);
TimePoint start, end;
for(int j = 0; j < 3; j++) {
start = system_clock::now();
for(auto it = m.begin(); it != m.end(); it++) {
if(j==2)
(*it).second += j;
}
end = system_clock::now();
}
Milli elapsed = end - start;
std::cout << "Iterating across " << count << " elements in a map of size " << count << " took " << elapsed.count() << " milliseconds time per iteration was " << elapsed.count()/double(count)*1e6 << " nanoseconds" << std::endl;
}
template <typename T>
void copyTest(int count) {
using namespace std::chrono;
T m = ascendingInsert<T>(count,false);
TimePoint start, end;
start = system_clock::now();
T m2(m);
end = system_clock::now();
Milli elapsed = end - start;
std::cout << "Copy construction of a map of size " << m2.size() << " took " << elapsed.count() << " milliseconds" << std::endl;
}
/*
#include <assert.h>
using namespace std;
ostream &
operator<<(ostream &os, const type_info &ti) {
int ec;
const char *demangled_name = abi::__cxa_demangle(ti.name(), 0, 0, &ec);
assert(ec == 0);
os << demangled_name;
free((void *) demangled_name);
return os;
}
template <typename T>
void foo(T &&o) {
//o = 2;
cout << typeid(const int &) << endl;
}
int main() {
const int i = 1;
foo(i);
}
*/
class comma_numpunct : public std::numpunct<char> {
protected:
virtual char do_thousands_sep() const { return ','; }
virtual std::string do_grouping() const { return "\03"; }
};
int main() {
//separate all printed numbers with commas
std::locale comma_locale(std::locale(), new comma_numpunct());
std::cout.imbue(comma_locale);
auto demangle = [](const std::type_info &ti) {
int ec;
return abi::__cxa_demangle(ti.name(), 0, 0, &ec);
assert(ec == 0);
};
const char *w = demangle(typeid(cs440::StdMapWrapper<int,int>));
const char *m = demangle(typeid(cs440::Map<int,int>));
{
dispTestName("Ascending insert", m);
ascendingInsert<cs440::Map<int,int>>(1000);
ascendingInsert<cs440::Map<int,int>>(10000);
ascendingInsert<cs440::Map<int,int>>(100000);
ascendingInsert<cs440::Map<int,int>>(1000000);
ascendingInsert<cs440::Map<int,int>>(10000000);
dispTestName("Ascending insert", w);
ascendingInsert<cs440::StdMapWrapper<int,int>>(1000);
ascendingInsert<cs440::StdMapWrapper<int,int>>(10000);
ascendingInsert<cs440::StdMapWrapper<int,int>>(100000);
ascendingInsert<cs440::StdMapWrapper<int,int>>(1000000);
ascendingInsert<cs440::StdMapWrapper<int,int>>(10000000);
}
{
dispTestName("Descending insert", m);
descendingInsert<cs440::Map<int,int>>(1000);
descendingInsert<cs440::Map<int,int>>(10000);
descendingInsert<cs440::Map<int,int>>(100000);
descendingInsert<cs440::Map<int,int>>(1000000);
descendingInsert<cs440::Map<int,int>>(10000000);
dispTestName("Descending insert", w);
descendingInsert<cs440::StdMapWrapper<int,int>>(1000);
descendingInsert<cs440::StdMapWrapper<int,int>>(10000);
descendingInsert<cs440::StdMapWrapper<int,int>>(100000);
descendingInsert<cs440::StdMapWrapper<int,int>>(1000000);
descendingInsert<cs440::StdMapWrapper<int,int>>(10000000);
}
{
dispTestName("Delete test", m);
deleteTest<cs440::Map<int,int>>();
dispTestName("Delete test", w);
deleteTest<cs440::StdMapWrapper<int,int>>();
}
{
dispTestName("Find test", m);
findTest<cs440::Map<int,int>>();
dispTestName("Find test", w);
findTest<cs440::StdMapWrapper<int,int>>();
}
/*
Remember that some of these maps get quite large - iteration times may be affected by things other than the scaling of your algorithm.
How do the many levels of the memory heirarchy in a computer relate?
How do they perform relative to one another?
How might this have affected other performance tests?
*/
{
dispTestName("Iteration test", m);
iterationTest<cs440::Map<int,int>>(10000);
iterationTest<cs440::Map<int,int>>(20000);
iterationTest<cs440::Map<int,int>>(40000);
iterationTest<cs440::Map<int,int>>(80000);
iterationTest<cs440::Map<int,int>>(160000);
iterationTest<cs440::Map<int,int>>(320000);
iterationTest<cs440::Map<int,int>>(640000);
iterationTest<cs440::Map<int,int>>(1280000);
iterationTest<cs440::Map<int,int>>(2560000);
iterationTest<cs440::Map<int,int>>(5120000);
#if DO_BIG_ITERATION_TEST
//Optional test. This is more ram than the remote machines have and will likely take a long time to run.
iterationTest<cs440::Map<int,int>>(600000000);
#endif
dispTestName("Iteration test", w);
iterationTest<cs440::StdMapWrapper<int,int>>(10000);
iterationTest<cs440::StdMapWrapper<int,int>>(20000);
iterationTest<cs440::StdMapWrapper<int,int>>(40000);
iterationTest<cs440::StdMapWrapper<int,int>>(80000);
iterationTest<cs440::StdMapWrapper<int,int>>(160000);
iterationTest<cs440::StdMapWrapper<int,int>>(320000);
iterationTest<cs440::StdMapWrapper<int,int>>(640000);
iterationTest<cs440::StdMapWrapper<int,int>>(1280000);
iterationTest<cs440::StdMapWrapper<int,int>>(5120000);
#if DO_BIG_ITERATION_TEST
//Optional test. This is more ram than the remote machines have and will likely take a long time to run.
iterationTest<cs440::Map<int,int>>(600000000);
#endif
}
{
//Test copy constructor scaling
dispTestName("Copy test", m);
copyTest<cs440::Map<int,int>>(10000);
copyTest<cs440::Map<int,int>>(100000);
copyTest<cs440::Map<int,int>>(1000000);
copyTest<cs440::Map<int,int>>(10000000);
dispTestName("Copy test", w);
copyTest<cs440::StdMapWrapper<int,int>>(10000);
copyTest<cs440::StdMapWrapper<int,int>>(100000);
copyTest<cs440::StdMapWrapper<int,int>>(1000000);
copyTest<cs440::StdMapWrapper<int,int>>(10000000);
}
//Add your own indexibility scaling test here
// Cast, due to const-ness.
free((void *) w);
free((void *) m);
}