-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmemPoolTest.h
178 lines (157 loc) · 5.1 KB
/
memPoolTest.h
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
#include "pch.h"
#include "Utils.h"
template<typename T>
struct NodeT {
T value;
NodeT *next;
NodeT(const T& _value, NodeT *_next): value(_value), next(_next){}
};
template<typename T>
struct LinkedListSortAlgo {
typedef NodeT<T> Node;
virtual ~LinkedListSortAlgo() {}
virtual const char *getName() = 0;
virtual int getLimit() = 0;
virtual void sort(Node *begin, Node *end) = 0;
virtual void beginAlloc(int n) {}
virtual void endAlloc() {}
virtual Node* allocNode(int value, Node *next) { return new Node(value, next); }
virtual void freeNode(Node *n) { delete n; }
void assertSorted(Node *n) {
for (; n->next != nullptr; n = n->next) {
if (n->value > n->next->value) {
printf("sort failed! => %s\n", getName());
assert(0);
}
}
}
void run(T *begin, T *end) {
printf("%s: len=%.3fK\n", getName(), (end - begin) / 1024.0);
Node *head = nullptr;
{
double time = getTime();
beginAlloc(end - begin);
for (T *p = end - 1; p >= begin; --p) {
head = allocNode(*p, head);
}
printf("\tcreate list: %fs\n", getTime() - time);
}
{
double time = getTime();
sort(head, nullptr);
time = getTime() - time;
printf("\tsort : %fs, TPE=%fns\n", time, time * 1000000000 / (end - begin));
}
assertSorted(head);
{
double time = getTime();
for (Node *next; head != nullptr; head = next) {
next = head->next;
freeNode(head);
}
endAlloc();
printf("\tdestroy list: %fs\n", getTime() - time);
}
}
};
template<typename T>
class LinkedListSortAlgo_selectionSort: public LinkedListSortAlgo<T> {
public:
using typename LinkedListSortAlgo<T>::Node;
virtual const char *getName() { return "selectionSort"; }
virtual int getLimit() { return 16 * 1024; }
virtual void sort(Node *begin, Node *end) {
for (Node *p = begin; p != end; p = p->next) {
for (Node *q = p->next; q != end; q = q->next) {
if (q->value < p->value) swap(p->value, q->value);
}
}
}
};
template<typename T>
class LinkedListSortAlgo_selectionSortPool: public LinkedListSortAlgo_selectionSort<T> {
public:
using typename LinkedListSortAlgo_selectionSort<T>::Node;
virtual const char *getName() { return "selectionSortPool"; }
virtual Node* allocNode(int value, Node *next) {
return new (mPoolFree++) Node(value, next);
}
virtual void freeNode(Node *n) {
}
virtual void beginAlloc(int n) {
mPool = (Node*)malloc(sizeof(Node) * n);
mPoolFree = mPool;
}
virtual void endAlloc() {
free(mPool);
}
private:
Node *mPool;
Node *mPoolFree;
};
template<typename T>
class LinkedListSortAlgo_quickSort: public LinkedListSortAlgo<T> {
public:
using typename LinkedListSortAlgo<T>::Node;
virtual const char *getName() { return "quickSort"; }
virtual int getLimit() { return 1024 * 1024; }
virtual void sort(Node *begin, Node *end) {
if (begin == end || begin->next == end) return;
Node *mid = begin;
for (Node *p = begin->next; p != end; p = p->next) {
if (p->value <= begin->value) {
mid = mid->next;
swap(p->value, mid->value);
}
}
swap(begin->value, mid->value);
sort(begin, mid);
sort(mid->next, end);
}
};
template<typename T>
class LinkedListSortAlgo_quickSortPool: public LinkedListSortAlgo_quickSort<T> {
public:
using typename LinkedListSortAlgo_quickSort<T>::Node;
virtual const char *getName() { return "quickSortPool"; }
virtual Node* allocNode(int value, Node *next) {
return new (mPoolFree++) Node(value, next);
}
virtual void freeNode(Node *n) {
}
virtual void beginAlloc(int n) {
mPool = (Node*)malloc(sizeof(Node) * n);
mPoolFree = mPool;
}
virtual void endAlloc() {
free(mPool);
}
private:
Node *mPool;
Node *mPoolFree;
};
static void benchmark(vector<LinkedListSortAlgo<int>*> algos) {
int lens[] = {1, 5, 13, 19, 33, 65, 128, 255, 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024, 128 * 1024, 256 * 1024, 512 * 1024, 1024*1024};
for (int len : lens) {
vector<int> data(len);
for (int &d : data) d = myrand(len);
vector<int> tdata(data);
for (auto algo : algos) {
if (len > algo->getLimit()) continue;
tdata = data;
algo->run(&tdata[0], &tdata[0] + tdata.size());
}
}
}
int main() {
srand(time(nullptr));
setCpuAffinity(1);
vector<LinkedListSortAlgo<int>*> algos = {
new LinkedListSortAlgo_selectionSort<int>(),
new LinkedListSortAlgo_selectionSortPool<int>(),
new LinkedListSortAlgo_quickSort<int>(),
new LinkedListSortAlgo_quickSortPool<int>(),
};
benchmark(algos);
for (auto p : algos) delete p;
}