3. 内存池效率测试
3.1 测试1:连续申请和连续释放
分别用内存池和new连续申请和连续释放大量的内存块,比较其运行速度,代码如下:
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
using namespace std;
using namespace boost;
const int MAXLENGTH = 100000;
int main ( )
{
boost::pool<> p(sizeof(int));
int* vec1[MAXLENGTH];
int* vec2[MAXLENGTH];
clock_t clock_begin = clock();
for (int i = 0; i < MAXLENGTH; ++i)
{
vec1[i] = static_cast<int*>(p.malloc());
}
for (int i = 0; i < MAXLENGTH; ++i)
{
p.free(vec1[i]);
vec1[i] = NULL;
}
clock_t clock_end = clock();
cout<<"程序运行了 "<<clock_end-clock_begin<<" 个系统时钟"<<endl;
clock_begin = clock();
for (int i = 0; i < MAXLENGTH; ++i)
{
vec2[i] = new int;
}
for (int i = 0; i < MAXLENGTH; ++i)
{
delete vec2[i];
vec2[i] = NULL;
}
clock_end = clock();
cout<<"程序运行了 "<<clock_end-clock_begin<<" 个系统时钟"<<endl;
return 0;
}
#include <iostream>
#include <ctime>
#include <vector>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
using namespace std;
using namespace boost;
const int MAXLENGTH = 100000;
int main ( )
{
boost::pool<> p(sizeof(int));
int* vec1[MAXLENGTH];
int* vec2[MAXLENGTH];
clock_t clock_begin = clock();
for (int i = 0; i < MAXLENGTH; ++i)
{
vec1[i] = static_cast<int*>(p.malloc());
}
for (int i = 0; i < MAXLENGTH; ++i)
{
p.free(vec1[i]);
vec1[i] = NULL;
}
clock_t clock_end = clock();
cout<<"程序运行了 "<<clock_end-clock_begin<<" 个系统时钟"<<endl;
clock_begin = clock();
for (int i = 0; i < MAXLENGTH; ++i)
{
vec2[i] = new int;
}
for (int i = 0; i < MAXLENGTH; ++i)
{
delete vec2[i];
vec2[i] = NULL;
}
clock_end = clock();
cout<<"程序运行了 "<<clock_end-clock_begin<<" 个系统时钟"<<endl;
return 0;
}
测试环境:VS2008,WindowXP SP2,Pentium 4 CPU双核,1.5GB内存。
结论:在连续申请和连续释放10万块内存的情况下,使用内存池耗时是使用new耗时的47.46%。