-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
216 lines (181 loc) · 7.71 KB
/
main.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
#include "Allocator.h"
#include <assert.h>
#include <new>
#include <iostream>
// @see https://github.com/endurodave/Allocator
// On VisualStudio, to disable the debug heap for faster performance when using
// the debugger use this option:
// Debugging > Environment _NO_DEBUG_HEAP=1
class MyClass
{
DECLARE_ALLOCATOR
// remaining class definition
};
IMPLEMENT_ALLOCATOR(MyClass, 0, 0)
// Heap blocks mode unlimited with 100 byte blocks
Allocator allocatorHeapBlocks(100);
// Heap pool mode with 20, 100 byte blocks
Allocator allocatorHeapPool(100, 20);
// Static pool mode with 20, 100 byte blocks
char staticMemoryPool[100 * 20];
Allocator allocatorStaticPool(100, 20, staticMemoryPool);
// Static pool mode with 20 MyClass sized blocks using template
AllocatorPool<MyClass, 20> allocatorStaticPool2;
// Benchmark allocators
static const int MAX_BLOCKS = 10000;
static const int MAX_BLOCK_SIZE = 4096;
void* memoryPtrs[MAX_BLOCKS];
void* memoryPtrs2[MAX_BLOCKS];
AllocatorPool<char[MAX_BLOCK_SIZE], MAX_BLOCKS*2> allocatorStaticPoolBenchmark;
Allocator allocatorHeapBlocksBenchmark(MAX_BLOCK_SIZE);
static void out_of_memory()
{
// new-handler function called by Allocator when pool is out of memory
assert(0);
}
typedef void* (*AllocFunc)(int size);
typedef void (*DeallocFunc)(void* ptr);
void Benchmark(const char* name, AllocFunc allocFunc, DeallocFunc deallocFunc);
void* AllocHeap(int size);
void DeallocHeap(void* ptr);
void* AllocStaticPool(int size);
void DeallocStaticPool(void* ptr);
void* AllocHeapBlocks(int size);
void DeallocHeapBlocks(void* ptr);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(void)
{
std::set_new_handler(out_of_memory);
// Allocate MyClass using fixed block allocator
MyClass* myClass = new MyClass();
delete myClass;
// Allocate 100 bytes in fixed block allocator, then deallocate
void* memory1 = allocatorHeapBlocks.Allocate(100);
allocatorHeapBlocks.Deallocate(memory1);
void* memory2 = allocatorHeapBlocks.Allocate(100);
allocatorHeapBlocks.Deallocate(memory2);
void* memory3 = allocatorHeapPool.Allocate(100);
allocatorHeapPool.Deallocate(memory3);
void* memory4 = allocatorStaticPool.Allocate(100);
allocatorStaticPool.Deallocate(memory4);
void* memory5 = allocatorStaticPool2.Allocate(sizeof(MyClass));
allocatorStaticPool2.Deallocate(memory5);
Benchmark("Heap (Run 1)", AllocHeap, DeallocHeap);
Benchmark("Heap (Run 2)", AllocHeap, DeallocHeap);
Benchmark("Heap (Run 3)", AllocHeap, DeallocHeap);
Benchmark("Static Pool (Run 1)", AllocStaticPool, DeallocStaticPool);
Benchmark("Static Pool (Run 2)", AllocStaticPool, DeallocStaticPool);
Benchmark("Static Pool (Run 3)", AllocStaticPool, DeallocStaticPool);
Benchmark("Heap Blocks (Run 1)", AllocHeapBlocks, DeallocHeapBlocks);
Benchmark("Heap Blocks (Run 2)", AllocHeapBlocks, DeallocHeapBlocks);
Benchmark("Heap Blocks (Run 3)", AllocHeapBlocks, DeallocHeapBlocks);
return 0;
}
//------------------------------------------------------------------------------
// AllocHeap
//------------------------------------------------------------------------------
void* AllocHeap(int size)
{
return new CHAR[size];
}
//------------------------------------------------------------------------------
// DeallocHeap
//------------------------------------------------------------------------------
void DeallocHeap(void* ptr)
{
delete [] ptr;
}
//------------------------------------------------------------------------------
// AllocStaticPool
//------------------------------------------------------------------------------
void* AllocStaticPool(int size)
{
return allocatorStaticPoolBenchmark.Allocate(size);
}
//------------------------------------------------------------------------------
// DeallocStaticPool
//------------------------------------------------------------------------------
void DeallocStaticPool(void* ptr)
{
allocatorStaticPoolBenchmark.Deallocate(ptr);
}
//------------------------------------------------------------------------------
// AllocHeapBlocks
//------------------------------------------------------------------------------
void* AllocHeapBlocks(int size)
{
return allocatorHeapBlocksBenchmark.Allocate(size);
}
//------------------------------------------------------------------------------
// DeallocHeapBlocks
//------------------------------------------------------------------------------
void DeallocHeapBlocks(void* ptr)
{
allocatorHeapBlocksBenchmark.Deallocate(ptr);
}
//------------------------------------------------------------------------------
// Benchmark
//------------------------------------------------------------------------------
void Benchmark(const char* name, AllocFunc allocFunc, DeallocFunc deallocFunc)
{
#if WIN32
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds, TotalElapsedMicroseconds= {0};
LARGE_INTEGER Frequency;
SetProcessPriorityBoost(GetCurrentProcess(), true);
QueryPerformanceFrequency(&Frequency);
// Allocate MAX_BLOCKS blocks MAX_BLOCK_SIZE / 2 sized blocks
QueryPerformanceCounter(&StartingTime);
for (int i=0; i<MAX_BLOCKS; i++)
memoryPtrs[i] = allocFunc(MAX_BLOCK_SIZE / 2);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " allocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Deallocate MAX_BLOCKS blocks (every other one)
QueryPerformanceCounter(&StartingTime);
for (int i=0; i<MAX_BLOCKS; i+=2)
deallocFunc(memoryPtrs[i]);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " deallocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Allocate MAX_BLOCKS blocks MAX_BLOCK_SIZE sized blocks
QueryPerformanceCounter(&StartingTime);
for (int i=0; i<MAX_BLOCKS; i++)
memoryPtrs2[i] = allocFunc(MAX_BLOCK_SIZE);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " allocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Deallocate MAX_BLOCKS blocks (every other one)
QueryPerformanceCounter(&StartingTime);
for (int i=1; i<MAX_BLOCKS; i+=2)
deallocFunc(memoryPtrs[i]);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " deallocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Deallocate MAX_BLOCKS blocks
QueryPerformanceCounter(&StartingTime);
for (int i=MAX_BLOCKS-1; i>=0; i--)
deallocFunc(memoryPtrs2[i]);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " deallocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
std::cout << name << " TOTAL TIME: " << TotalElapsedMicroseconds.QuadPart << std::endl;
SetProcessPriorityBoost(GetCurrentProcess(), false);
#endif
}