为了测试几种常用的原生多维数组管理方法,我们利用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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
输出结果如下:
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
可见,使用 shared_ptr 还是很划算的!用array、vector也不赖!