测试boost内存池

最近测试了一下boost线程池,向大家公布一下测试结果,本测试在单线程环境下进行,

 boost::pool实用单线程内存方法

 boost::singleton_pool适用多线程内存访问

测试代码如下


struct Student
{
	char name[8];
};

void heapAllocate()
{
	CExecTimer timer;

	Student* array[1000];

	for (int i=0;i<5000;i++)
	{
		for (int j=0;j<1000;j++)
		{
			array[j]=new Student;
		}

		for (int j=0;j<1000;j++)
		{
			delete array[j];
		}
	}
}

void boostAllocate()
{
	boost::pool<> student_pool(sizeof(Student));

	CExecTimer timer;

	Student* array[1000];

	for (int i=0;i<5000;i++)
	{
		for (int j=0;j<1000;j++)
		{
			array[j]=(Student*)student_pool.malloc();
		}

		for (int j=0;j<1000;j++)
		{
			student_pool.free(array[j]);
		}
	}
}

typedef struct singleton_pool_tag
{

}singleton_pool_tag;

typedef boost::singleton_pool<singleton_pool_tag,sizeof(Student)>  global;

void boostSingletonAllocate()
{
	CExecTimer timer;

	Student* array[1000];

	for (int i=0;i<5000;i++)
	{
		for (int j=0;j<1000;j++)
		{
			array[j]=(Student*)global::malloc();
		}

		for (int j=0;j<1000;j++)
		{
			global::free(array[j]);
		}
	}
}

其中更改name数组的大小来设置分配单元大小



分配单元大小普通堆分配用时boost::pool用时boost::singleton_pool用时
8字节2620 milli seconds1420 milli seconds4493 milli seconds

分配单元大小普通堆分配用时boost::pool用时boost::singleton_pool用时
128字节2871 milli seconds1419 milli seconds4508 milli seconds

分配单元大小普通堆分配用时 boost::pool用时boost::singleton_pool用时
256字节 3136 milli seconds1419 milli seconds4509 milli seconds
1K4306 milli seconds1435 milli seconds4539 milli seconds
2K9048 milli seconds1451 milli seconds4555 milli seconds



分配单元大小普通堆分配用时boost::pool用时boost::singleton_pool用时
4K20671 milli seconds1779 milli seconds4930 milli seconds

分配单元大小普通堆分配用时boost::pool用时boost::singleton_pool用时
8K20671 milli seconds1779 milli seconds4930 milli seconds


有以上数据分析出,boost::pool性能始终最优(因其无锁),当分配单元小于1K时,boost:: singleton_pool性能甚至比不过正常的内存分配,当分配单元大于1K时,

boost::singleton_pool的优势才会逐渐显示

若有同学认为有需要探讨的地方,一定要留言哦!

PS, 今天在自己的机器上又测试了一下,临界值变为2K,faint!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值