share_ptr在C++多维数组管理方法中的优势和性能测试

为了测试几种常用的原生多维数组管理方法,我们利用GNU C++ 7.2 最新的MSYS2版本,进行一个测试:
主要测试涵盖指针型的二维数组、vector\array\map

#include <vector>
#include <array>
#include <functional>
#include <map>
#include <unordered_map>
#include <stdio.h>
#include <time.h>
#include <malloc.h>
#include <memory>
#include <typeinfo>
//GNU C++ 7.2
//链接选项: -Wl,--stack,20240000
using namespace std;
const int testloopTimes = 1000;
const int d1 = 80;
const int d2 = 60;
const int d3 = 40;
const int d4 = 20;
//用于进行循环赋值测试的函数
template <typename T>
double function_loop(T p, int Ntimes)
{
	double  duration = 0;
	clock_t start, finish;
	start=clock();
	for (int c=0;c<Ntimes;++c)
		for(int h=0; h<d1; h++)
			for(int i=0; i<d2; i++)
				for(int j=0; j<d3; j++)
					for(int k=0; k<d4; k++)
						p[h][i][j][k]=h*60+i*20+j*5+k + c;
	finish=clock();
	duration=(double)(finish-start)/CLOCKS_PER_SEC;
	if (Ntimes>1)
		printf("\tloop cost: %f (s)\n",duration);
	return duration;
}
//用于测试含有构造、析构的函数
void test(function<double (void)> ftest)
{
	double  duration = 0;
	clock_t start, finish;
	start=clock();
	double loopCost = ftest();
	finish=clock();
	duration=(double)(finish-start)/CLOCKS_PER_SEC;
	printf("\tMEM  cost: %f (s)\n",duration - loopCost );
}
//vector 测试
double function_vector()
{
	vector<vector<vector<vector<int> > > >
			p(d1,vector<vector<vector<int> > >
			  (d2,vector<vector<int> >
			   (d3,vector<int>(d4,0))));
	printf ("test vector:\n");
	double loopCost = function_loop(p,testloopTimes);
	return loopCost;
}
//array 测试
double function_array()
{
	array<array<array<array<int,d4 >,d3 >,d2 >,d1> p{0};
	printf ("test array:\n");
	double loopCost = function_loop(p,testloopTimes);
	return loopCost;
}
//map 测试
double function_map()
{
	map<int,map<int,map<int,map<int,int > > >> p;
	printf ("test map:\n");
	function_loop(p,1);
	double loopCost = function_loop(p,testloopTimes);
	return loopCost;
}
//unordered_map 测试
double function_unordered_map()
{
	unordered_map<int,unordered_map<int,unordered_map
			<int,unordered_map<int,int > > >> p;
	printf ("test unordered_map:\n");
	function_loop(p,1);
	double loopCost = function_loop(p,testloopTimes);
	return loopCost;
}
//shared_ptr 测试
#if __GNUC__>= 7
double function_shared_ptr()
{
	shared_ptr<int[][d2][d3][d4]> p (new int [d1][d2][d3][d4]);
	printf ("test shared_ptr:\n");
	double loopCost = function_loop(p,testloopTimes);
	return loopCost;
}
#endif

//pointer测试
double function_pointer()
{
	printf ("test pointer:\n");
	int ****p = nullptr;
	int h,i,j;
	p=new int ***[d1];
	if (nullptr==p) return 0;
	for (h=0; h<d1; h++)
	{
		p[h]=new int ** [d2];
		if (nullptr==p[h]) return 0;
		for (i=0; i<d2; i++)
		{
			p[h][i]=new int *[d3];
			if (nullptr==p[h][i]) return 0;
			for (j=0; j<d3; j++)
			{
				p[h][i][j]=new int [d4];
				if (nullptr==p[h][i][j]) return 0;
			}
		}
	}
	double loopCost = function_loop(p,testloopTimes);
	for (h=0; h<d1; h++)
	{
		for (i=0; i<d2; i++)
		{
			for (j=0; j<d3; j++)
			{
				delete [] p[h][i][j];
			}
			delete [] p[h][i];
		}
		delete [] p[h];
	}
	delete [] p;
	return loopCost;
}

int main()
{
	test(function_vector);
	test(function_array);
	test(function_pointer);
	test(function_map);
	test(function_unordered_map);
#if __GNUC__>= 7
	test(function_shared_ptr);
#endif
	return 0;
}



输出结果如下:

test vector:
        loop cost: 1.594000 (s)
        MEM  cost: 0.046000 (s)
test array:
        loop cost: 1.204000 (s)
        MEM  cost: 0.000000 (s)
test pointer:
        loop cost: 1.500000 (s)
        MEM  cost: 0.015000 (s)
test map:
        loop cost: 156.454000 (s)
        MEM  cost: 0.563000 (s)
test unordered_map:
        loop cost: 83.639000 (s)
        MEM  cost: 0.609000 (s)
test shared_ptr:
        loop cost: 1.219000 (s)
        MEM  cost: 0.015000 (s)

可见,使用 shared_ptr 还是很划算的!用array、vector也不赖!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁劲犇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值