/*
** 功能:在list 和vector 中插入1百万个上述类型所花的时间以秒为单位 表
**
** 作者:genii
** 描述:正如你所看到的对于小的数据类型vector的性能要比list好得多,而对于大型的数
** 据类型则相反list的性能要好得多,区别是由于vector 需要重新增长以及拷贝元素。
** 但是数据类型的长度不是影响容器性能的惟一标准
**
**
*/
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <string.h>
#include <time.h>
using namespace std;
#define MAX 10000000
time_t _begin;
time_t _end;
//向vector中插入1000000条数据
template<class T>
void v_insert_int(T a)
{
vector<T> v;
for( int i = 0; i < MAX; i++ )
{
v.push_back( a );
}
}
//向list中插入1000000条数据
template<class T>
void l_insert_int(T a)
{
list<T> l;
for( int i = 0; i < MAX; i++ )
{
l.push_back( a );
}
}
//一个复杂类
class CComplex
{
private:
void A();
void B();
void C();
void A1();
void B1();
void C1();
void A2();
void B2();
void C2();
public:
int a;
int b;
double c;
double d;
string e;
int a1;
int b1;
double c1;
double d1;
string e1;
int a2;
int b2;
double c2;
double d2;
string e2;
};
//一个简单类
class CSimple
{
private:
void A();
public:
int a;
double c;
};
int main()
{
//Int类型 vector时间
_begin = clock();
v_insert_int(123);
_end = clock();
cout<< "Int vector:" <<(_end-_begin)/1000.0 <<endl;
//double 类型 vector时间
_begin = clock();
v_insert_int(123.0);
_end = clock();
cout<< "double vector:" <<(_end-_begin)/1000.0 <<endl;
//string 类型 vector时间
_begin = clock();
v_insert_int("string");
_end = clock();
cout<< "string vector:" <<(_end-_begin)/1000.0 <<endl;
//简单类 vector时间
CSimple cs;
_begin = clock();
v_insert_int(cs);
_end = clock();
cout<< "CSimple vector:" <<(_end-_begin)/1000.0 <<endl;
//复杂类 vector时间
CComplex cc;
_begin = clock();
v_insert_int(cc);
_end = clock();
cout<< "CComplex vector:" <<(_end-_begin)/1000.0 <<endl;
cout<<endl;
//Int类型 list时间
_begin = clock();
l_insert_int(123);
_end = clock();
cout<< "Int list:" <<(_end-_begin)/1000.0 <<endl;
//double 类型 list时间
_begin = clock();
l_insert_int(123.0);
_end = clock();
cout<< "double list:" <<(_end-_begin)/1000.0 <<endl;
//string 类型 list时间
_begin = clock();
l_insert_int("string");
_end = clock();
cout<< "string list:" <<(_end-_begin)/1000.0 <<endl;
//简单类 list时间
_begin = clock();
l_insert_int(cs);
_end = clock();
cout<< "CSimple list:" <<(_end-_begin)/1000.0 <<endl;
//复杂类 list时间
_begin = clock();
l_insert_int(cc);
_end = clock();
cout<< "CComplex list:" <<(_end-_begin)/1000.0 <<endl;
}
运行结果:
![](https://img-blog.csdn.net/20130821224139515?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2VuaWkxMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
分析:
vector在对于小的数据类型的性能要比list好得多,而对于大型的复杂的数据类型则相反list的性能要好得多,区别是由于vector 需要重新增长以及拷贝元素。
无论是list 还是vector 对于已定义拷贝构造函数的类来说插入这样的类的元素都需要调用拷贝构造函数。另外随着每次重新分配内存vector 必须为每个元素调用拷贝构造函数而且在释放原来的内存时它要为每个元素调用其相关类型的析构函数,vector 的动态自我增长越频繁元素插入的开销就越大。
解决方案:
第一种: 当vector 的开销变得非常大时把vector 转换成list
第二种: 容量从1增加到4096,因此重新分配的次数大大减少其次指向类对象的指针的拷贝和释放不需要调用该类的拷贝构造函数和析构函数
在template<class T>void v_insert_int(T a)
template<class T> void v_insert_int(T a) { vector<T> v; v.reserve(10000000); //添加此行 调整vector 的容量 for( int i = 0; i < MAX; i++ ) { v.push_back( a ); } }
结果将会发生改变:
![](https://img-blog.csdn.net/20130821224303296?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2VuaWkxMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
结束!!!