-
Notifications
You must be signed in to change notification settings - Fork 411
/
test.cpp
139 lines (126 loc) · 4.63 KB
/
test.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
/*-
* Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/*-
* Provided to compare the default allocator to MemoryPool
*
* Check out StackAlloc.h for a stack implementation that takes an allocator as
* a template argument. This may give you some idea about how to use MemoryPool.
*
* This code basically creates two stacks: one using the default allocator and
* one using the MemoryPool allocator. It pushes a bunch of objects in them and
* then pops them out. We repeat the process several times and time how long
* this takes for each of the stacks.
*
* Do not forget to turn on optimizations (use -O2 or -O3 for GCC). This is a
* benchmark, we want inlined code.
*/
#include <iostream>
#include <cassert>
#include <time.h>
#include <vector>
#include "MemoryPool.h"
#include "StackAlloc.h"
/* Adjust these values depending on how much you trust your computer */
#define ELEMS 1000000
#define REPS 50
int main()
{
clock_t start;
std::cout << "Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com\n";
std::cout << "Provided 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++) {
// Unroll to time the actual code and not the loop
stackDefault.push(i);
stackDefault.push(i);
stackDefault.push(i);
stackDefault.push(i);
}
for (int i = 0; i < ELEMS / 4; i++) {
// Unroll to time the actual code and not the loop
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++) {
// Unroll to time the actual code and not the loop
stackPool.push(i);
stackPool.push(i);
stackPool.push(i);
stackPool.push(i);
}
for (int i = 0; i < ELEMS / 4; i++) {
// Unroll to time the actual code and not the loop
stackPool.pop();
stackPool.pop();
stackPool.pop();
stackPool.pop();
}
}
std::cout << "MemoryPool Allocator Time: ";
std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";
std::cout << "Here is a secret: the best way of implementing a stack"
" is a dynamic array.\n";
/* Compare MemoryPool to std::vector */
std::vector<int> stackVector;
start = clock();
for (int j = 0; j < REPS; j++)
{
assert(stackVector.empty());
for (int i = 0; i < ELEMS / 4; i++) {
// Unroll to time the actual code and not the loop
stackVector.push_back(i);
stackVector.push_back(i);
stackVector.push_back(i);
stackVector.push_back(i);
}
for (int i = 0; i < ELEMS / 4; i++) {
// Unroll to time the actual code and not the loop
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";
std::cout << "The vector implementation will probably be faster.\n\n";
std::cout << "MemoryPool still has a lot of uses though. Any type of tree"
" and when you have multiple linked lists are some examples (they"
" can all share the same memory pool).\n";
return 0;
}