Skip to content

Commit 79db4ce

Browse files
committed
Create test.cpp
1 parent 5ee8687 commit 79db4ce

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

test.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*-
2+
* Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
/*-
24+
* Provided to compare the default allocator to MemoryPool
25+
*
26+
* Check out StackAlloc.h for a stack implementation that takes an allocator as
27+
* a template argument. This may give you some idea about how to use MemoryPool.
28+
*
29+
* This code basically creates two stacks: one using the default allocator and
30+
* one using the MemoryPool allocator. It pushes a bunch of objects in them and
31+
* then pops them out. We repeat the process several times and time how long
32+
* this takes for each of the stacks.
33+
*
34+
* Do not forget to turn on optimizations (use -O2 or -O3 for GCC). This is a
35+
* benchmark, we want inlined code.
36+
*/
37+
38+
#include <iostream>
39+
#include <cassert>
40+
#include <time.h>
41+
#include <vector>
42+
43+
#include "MemoryPool.h"
44+
#include "StackAlloc.h"
45+
46+
/* Adjust these values depending on how much you trust your computer */
47+
#define ELEMS 1000000
48+
#define REPS 50
49+
50+
int main()
51+
{
52+
clock_t start;
53+
54+
std::cout << "Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com\n";
55+
std::cout << "Provided to compare the default allocator to MemoryPool.\n\n";
56+
57+
/* Use the default allocator */
58+
StackAlloc<int, std::allocator<int> > stackDefault;
59+
start = clock();
60+
for (int j = 0; j < REPS; j++)
61+
{
62+
assert(stackDefault.empty());
63+
for (int i = 0; i < ELEMS / 4; i++) {
64+
// Unroll to time the actual code and not the loop
65+
stackDefault.push(i);
66+
stackDefault.push(i);
67+
stackDefault.push(i);
68+
stackDefault.push(i);
69+
}
70+
for (int i = 0; i < ELEMS / 4; i++) {
71+
// Unroll to time the actual code and not the loop
72+
stackDefault.pop();
73+
stackDefault.pop();
74+
stackDefault.pop();
75+
stackDefault.pop();
76+
}
77+
}
78+
std::cout << "Default Allocator Time: ";
79+
std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";
80+
81+
/* Use MemoryPool */
82+
StackAlloc<int, MemoryPool<int> > stackPool;
83+
start = clock();
84+
for (int j = 0; j < REPS; j++)
85+
{
86+
assert(stackPool.empty());
87+
for (int i = 0; i < ELEMS / 4; i++) {
88+
// Unroll to time the actual code and not the loop
89+
stackPool.push(i);
90+
stackPool.push(i);
91+
stackPool.push(i);
92+
stackPool.push(i);
93+
}
94+
for (int i = 0; i < ELEMS / 4; i++) {
95+
// Unroll to time the actual code and not the loop
96+
stackPool.pop();
97+
stackPool.pop();
98+
stackPool.pop();
99+
stackPool.pop();
100+
}
101+
}
102+
std::cout << "MemoryPool Allocator Time: ";
103+
std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";
104+
105+
106+
std::cout << "Here is a secret: the best way of implementing a stack"
107+
" is a dynamic array.\n";
108+
109+
/* Compare MemoryPool to std::vector */
110+
std::vector<int> stackVector;
111+
start = clock();
112+
for (int j = 0; j < REPS; j++)
113+
{
114+
assert(stackVector.empty());
115+
for (int i = 0; i < ELEMS / 4; i++) {
116+
// Unroll to time the actual code and not the loop
117+
stackVector.push_back(i);
118+
stackVector.push_back(i);
119+
stackVector.push_back(i);
120+
stackVector.push_back(i);
121+
}
122+
for (int i = 0; i < ELEMS / 4; i++) {
123+
// Unroll to time the actual code and not the loop
124+
stackVector.pop_back();
125+
stackVector.pop_back();
126+
stackVector.pop_back();
127+
stackVector.pop_back();
128+
}
129+
}
130+
std::cout << "Vector Time: ";
131+
std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";
132+
133+
std::cout << "The vector implementation will probably be faster.\n\n";
134+
std::cout << "MemoryPool still has a lot of uses though. Any type of tree"
135+
" and when you have multiple linked lists are some examples (they"
136+
" can all share the same memory pool).\n";
137+
138+
return 0;
139+
}

0 commit comments

Comments
 (0)