Repository navigation
Expand file tree
/
Copy pathmptest.cc
More file actions
89 lines (83 loc) · 1.91 KB
/
Copy pathmptest.cc
File metadata and controls
89 lines (83 loc) · 1.91 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
#include <time.h>
#include <vector>
#include <cassert>
#include <iostream>
#include "MemoryPool.h"
#include "StackAlloc.h"
#define ELEMS 1000000
#define REPS 50
int int main(int argc, char const *argv[])
{
/* code */
clock_t start;
std::cout << "Provide to compare the default allocator to MemoryPool. \n\n";
/*Use the default allocator*/
StackAlloc<int, std::allocator<int> > stackDefault;
start = clock();
for (int j = 0; j < REPS; j++)
{
assert(stackDefault.empty());
for (int i = 0; i < ELEMS / 4; i++)
{
stackDefault.push(i);
stackDefault.push(i);
stackDefault.push(i);
stackDefault.push(i);
}
for (int i = 0; i < ELEMS / 4; i++)
{
stackDefault.pop();
stackDefault.pop();
stackDefault.pop();
stackDefault.pop();
}
}
std::cout << "Default Allocator Time: ";
std::cout << (((double)clock()- start) / CLOCKS_PER_SEC) << "\n\n";
/*Use MemoryPool*/
StackAlloc<int, MemoryPool<int> > stackPool;
start = clock();
for (int j = 0; j < REPS; j++)
{
assert(stackPool.empty());
for (int i = 0; i < ELEMS / 4; i++)
{
stackPool.push(i);
stackPool.push(i);
stackPool.push(i);
stackPool.push(i);
}
for (int i = 0; i < ELEMS / 4; i++)
{
stackPool.pop();
stackPool.pop();
stackPool.pop();
stackPool.pop();
}
}
std::cout << "MemoryPool Allocator Time: ";
std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";
std::vector<int> stackVector;
start = clock();
for (int j = 0; j < REPS; j++)
{
assert(stackVector.empty());
for (int i = 0; i < ELEMS / 4; i++)
{
stackVector.push_back(i);
stackVector.push_back(i);
stackVector.push_back(i);
stackVector.push_back(i);
}
for (int i = 0; i < ELEMS / 4; i++)
{
stackVector.pop_back();
stackVector.pop_back();
stackVector.pop_back();
stackVector.pop_back();
}
}
std::cout << "Vector time: ";
std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";
return 0;
}