vector与内部数组性能差异
程序员文章站
2022-05-20 10:31:26
...
// array.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <ctime>
#include <iostream>
using namespace std;
void main()
{
clock_t t1,t2;
int tmp[256][256];
int a[256][256];
vector < vector <int> > vtmp;
for (int i = 0; i < 256; ++i) {
vector <int> t;
vtmp.push_back(t);
for (int j = 0; j < 256; ++j) {
tmp[i][j] = j + 1;
vtmp[i].push_back(j + 1);
}
}
t1 = clock();
for(int n = 0; n < 4096; n++)
for(int i = 0; i < 256; i++)
for(int j = 0; j < 256; j++)
{
a[i][j] = tmp[i][j];
a[i][j] += tmp[i][j];
a[i][j] /= tmp[i][j];
}
t2 = clock();
cout<< t2-t1 <<endl;
t1 = clock();
for(int n = 0; n < 4096; n++)
for(int i = 0; i < 256; i++)
for(int j = 0; j < 256; j++)
{
a[i][j] = vtmp[i][j];
a[i][j] += vtmp[i][j];
a[i][j] /= vtmp[i][j];
}
t2 = clock();
cout<< t2-t1 <<endl;
getchar();
}
VS2008下:
debug:1647 (111264)半天不出来!!
release:710 2039
不比不知道,STL在于全面性和便利性,在某些专门的计算(图像处理)性能不如内部数组(OpenCV)
推荐阅读