mem-pool VS malloc, new

#define BOOST_ALL_NO_LIB
//#define BOOST_LIB_DIAGNOSTIC
#include <iostream>
#include <boost/timer.hpp>
#include <boost/pool/pool.hpp>
#include <libs/system/src/error_code.cpp>

#define BL_SIZE 4
#define TEST_COUNT 100000000

void testMemoryPool()
{
	boost::pool<> memPool(BL_SIZE);
	boost::timer t;
	for (int i = 0; i < TEST_COUNT; ++i)
	{
	char *p = new char[BL_SIZE];
	delete[] p;
	}
	std::cout << "new and delete count: " << TEST_COUNT << ". use time: " << t.elapsed() << " sec." << std::endl;

	t.restart();
	for (int i = 0; i < TEST_COUNT; ++i)
	{
	void *p = malloc(BL_SIZE);
	free(p);
	}
	std::cout << "system malloc and free count: " << TEST_COUNT << ". use time: " << t.elapsed() << " sec." << std::endl;

	t.restart();
	for (int i = 0; i < TEST_COUNT; ++i)
	{
	void *p = memPool.malloc();
	memPool.free(p);
	}
	std::cout << "mempool malloc and free count: " << TEST_COUNT << ". use time: " << t.elapsed() << " sec." << std::endl;
}

int main(void)
{
	testMemoryPool();
	return 0;
}

测试结果(Release版):mem-pool VS malloc, new

 

你可能感兴趣的:(malloc,new,boost,pool,mem)