-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemPool.cpp
71 lines (60 loc) · 1.44 KB
/
MemPool.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
// main.cpp
// Cpp-Fundamentals
#include <iostream>
#include <chrono>
#include "MemPool.h"
using namespace std;
class Data
{
char m_data[4096];
};
void NoPool()
{
auto start = chrono::steady_clock::now();
for(unsigned int i=0; i < 1000000; i++)
{
Data *p = new Data;
delete p;
}
auto end = chrono::steady_clock::now();
cout << "Elapsed Time NoPool = " <<
chrono::duration_cast<chrono::milliseconds>(end - start).count() << " ms" << endl;
}
char buf[4096]; //Simple Memory Pool
class SharedData
{
public:
void *operator new(size_t uiSize)
{
return (void *)buf;
}
void operator delete(void *p)
{
}
};
void SimpleMemPool()
{
auto start = chrono::steady_clock::now();
for(unsigned int i=0; i < 1000000; i++)
{
SharedData *p = new SharedData;
delete p;
}
auto end = chrono::steady_clock::now();
cout << "Elapsed Time MemPool = " <<
chrono::duration_cast<chrono::milliseconds>(end - start).count() << " ms" << endl;
}
int main(int argc, const char * argv[]) {
cout << "Performance Test" << endl;
cout << "Without Pool" << endl;
NoPool();
cout << "With Pool" << endl;
SimpleMemPool();
unsigned long ulUnitNum = 4;
unsigned long ulUnitSize = 32;
MemPool* pool = new MemPool(ulUnitNum,ulUnitSize);
void* p1 = pool->Alloc(ulUnitSize);
pool->Free(p1);
delete pool;
return 0;
}