当服务程序需要频繁申请销毁对象时,为了提高性能,通常做法都会自己预先申请好内存池,以减少频繁系统调用可能造成的内存碎片。
分别使用了三种方式申请小内存对象
1. 默认的内存管理
2. 使用boost::pool分配管理内存
3. 自己实现的简单内存链表
4. 使用boost::object_pool创建对象
测试结果:
gcc 版本 4.1.2 20080704
0.02 s
0.03 s
0.03 s
3.64 s
测试结果显示glibc默认的小内存分配效率已经非常高了。
vs2010测试结果:
7.90 s
0.17 s
0.08 s
24.26 s
可见vs2010 默认内存分配效率非常低下,需要采取预分配内存池管理。其中不管是GCC还是VS2010下面测试,第四种方式效率都奇差,下篇文章会进行解释。
1 #include <boost/progress.hpp>
2
3 #include <boost/pool/poolfwd.hpp>
4 #include <boost/pool/pool.hpp>
5 #include <boost/pool/object_pool.hpp>
6
7
8 #include <iostream>
9 #include <stdio.h>
10
11
12 //##########################################
13
14 class RatinalClassTest
15 {
16 public:
17
18 RatinalClassTest():m_a(0), m_b(0)
19 {
20
21 }
22
23 RatinalClassTest(int a, int b):m_a(a), m_b(b)
24 {
25
26 }
27
28 private:
29 int m_a;
30 int m_b;
31
32 };
33
34 //############################################
35
36 class RatinalClassTest_pool : public RatinalClassTest
37 {
38 public:
39 RatinalClassTest_pool():RatinalClassTest()
40 {
41
42 }
43
44 void* operator new(size_t size)
45 {
46 return m_malloc_test.malloc();
47 }
48
49 void operator delete(void *p)
50 {
51 m_malloc_test.free(p);
52 }
53
54 private:
55
56 static boost::pool<> m_malloc_test;
57
58 };
59
60
61 boost::pool<> RatinalClassTest_pool::m_malloc_test(sizeof(RatinalClassTest_pool));
62
63
64 //################################
65 struct memlist
66 {
67 struct memlist *next;
68 };
69
70
71
72
73
74 class RatinalClassTest_selfpool : public RatinalClassTest
75 {
76 public:
77 RatinalClassTest_selfpool():RatinalClassTest()
78 {
79
80 }
81
82 void* operator new(size_t size)
83 {
84 return getnext();
85 }
86
87 void operator delete(void *p)
88 {
89 freemem(p);
90 }
91
92
93 static void* getnext()
94 {
95 void *p = NULL;
96
97 if (m_first == NULL)
98 {
99 memlistgrow();
100 }
101
102 p = m_first;
103 m_first = m_first->next;
104
105 return p;
106 }
107
108 static void freemem(void *p)
109 {
110 if (p)
111 {
112 struct memlist* temp = (struct memlist* )p;
113 temp->next = m_first;
114 m_first = temp;
115 }
116
117 }
118
119
120 static void memlistgrow()
121 {
122 int i = 0;
123
124 size_t memsize = sizeof(RatinalClassTest_selfpool) > sizeof(struct memlist* ) ? sizeof(RatinalClassTest_selfpool): sizeof(struct memlist* );
125
126 for (; i< MAX_MEM_BLOCK; i++)
127 {
128 struct memlist* pmem = (struct memlist*)malloc(sizeof(memsize));
129 pmem->next = m_first;
130 m_first = pmem;
131 }
132 }
133
134
135 static void memlistfree()
136 {
137 while (m_first)
138 {
139 struct memlist* pmem = m_first->next;
140 free(m_first);
141 m_first = pmem;
142 }
143
144 }
145
146
147 enum { MAX_MEM_BLOCK = 1000};
148
149 private:
150
151 static struct memlist* m_first;
152
153 };
154
155
156
157 struct memlist* RatinalClassTest_selfpool::m_first = NULL;
158
159
160
161
162 int main()
163 {
164 int i = 0;
165
166
167 {
168 RatinalClassTest * rtcarray[1000];
169 boost::progress_timer pt;
170
171 for (; i< 500; i++ )
172 {
173 int j = 0;
174 for (; j< 1000; j++)
175 {
176 rtcarray[j] = new RatinalClassTest;
177 }
178
179 j = 0;
180 for (; j< 1000; j++)
181 {
182 delete rtcarray[j] ;
183 }
184 }
185
186 }
187
188 //###################################################
189 {
190
191 RatinalClassTest_pool * rtcarray[1000];
192 boost::progress_timer pt;
193
194 i = 0;
195
196 for (; i< 500; i++ )
197 {
198 int j = 0;
199 for (; j< 1000; j++)
200 {
201 rtcarray[j] = new RatinalClassTest_pool;
202 }
203
204 j = 0;
205 for (; j< 1000; j++)
206 {
207 delete rtcarray[j] ;
208 }
209 }
210
211 }
212
213
214
215 //###################################################
216 {
217
218 RatinalClassTest_selfpool * rtcarray[1000];
219
220
221 RatinalClassTest_selfpool::memlistgrow();
222
223 boost::progress_timer pt;
224
225 i = 0;
226
227 for (; i< 500; i++ )
228 {
229 int j = 0;
230 for (; j< 1000; j++)
231 {
232 rtcarray[j] = new RatinalClassTest_selfpool;
233 }
234
235 j = 0;
236 for (; j< 1000; j++)
237 {
238 delete rtcarray[j] ;
239 }
240 }
241
242 RatinalClassTest_selfpool::memlistfree();
244 }
245
246
247
248 {
249
250 RatinalClassTest * rtcarray[1000];
251
252 boost::progress_timer pt;
253
254 boost::object_pool<RatinalClassTest> objectmempool;
255
256 i = 0;
257
258 for (; i< 500; i++ )
259 {
260 int j = 0;
261 for (; j< 1000; j++)
262 {
263 rtcarray[j] = objectmempool.construct();
264 }
265
266 j = 0;
267 for (; j< 1000; j++)
268 {
269 objectmempool.destroy(rtcarray[j]);
270 }
271 }
272 }
273
274
275 return 0;
276 }
277
posted on 2014-06-18 12:54
pizzx 阅读(507)
评论(0) 编辑 收藏 引用 所属分类:
c++/boost