下面是验证代码,往vector里面填入数据,实验后发现几个现象:
0. vector用赋值方式插入大量数据时最快,time3 是time1的10分之一
1. vector插入大量数据时,用reserve比不用效率高2 是1 的四分之一
2. 用vector先填入数据再sort比直接用set效率要高,用时只有四分之一
3. vector里面存结构体指针,内存用的少,大数据量时建议用
4 vector 里面用智能指针,不用考虑内存释放,std::shared_ptr<BigTestStruct>,但是用时较多
运行时间500000
Time 1:1104
Time 2:323
Time 3:85
Time 4:1571
Time 5:6943
Time 6:945
Time 7:1040
请按任意键继续. . .
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<complex>
#include<cstdlib>
#include<iostream>
#include<map>
#include<string>
#include<time.h>
#include <thread>
#include <chrono>
#include <iostream>
#include <windows.h>
#include<vector>
#include<set>
#include<iterator>
#include <mutex>
#include <algorithm>
#include <memory>
using namespace std;
struct BigTestStruct
{
int iValue = 1;
float fValue;
long lValue;
double dValue;
char cNameArr[10];
int iValArr[100];
};
bool compare2(const BigTestStruct& st1, const BigTestStruct& st2)
{
return st1.lValue < st2.lValue;
}
struct myComp
{
bool operator()(const BigTestStruct &a, const BigTestStruct &b)
{
return a.lValue - b.lValue > 0;
}
};
#define COUNT 5000000
int nnn1 = COUNT;
void FillVector(vector<BigTestStruct>& testVector)
{
for (int i = 0; i < COUNT; i++)
{
BigTestStruct bt;
bt.lValue = nnn1--;
testVector.emplace_back(bt);
}
}
int nnn2 = COUNT;
void FillVector2(vector<BigTestStruct>& testVector)
{
for (int i = 0; i < COUNT; i++)
{
BigTestStruct bt;
bt.lValue = nnn2--;
testVector[i] = bt;
}
}
int nnn3 = COUNT;
void FillSet1(set<BigTestStruct, myComp>& testSet)
{
for (int i = 0; i < COUNT; i++)
{
BigTestStruct bt;
bt.lValue = nnn3--;
testSet.insert(bt);
}
}
int nnn4 = COUNT;
void FillVector4(vector<BigTestStruct*>& testVector)
{
for (int i = 0; i < COUNT; i++)
{
BigTestStruct* bt = new BigTestStruct();
bt->lValue = nnn2--;
testVector.emplace_back(bt);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<BigTestStruct> testVector1;
vector<BigTestStruct> testVector2;
vector<BigTestStruct> testVector3(COUNT);
clock_t start = clock();
FillVector(testVector1);
testVector1.clear();
clock_t end1 = clock();
cout << "Time 1:" <<end1- start<< endl;
testVector2.reserve(COUNT);
FillVector(testVector2);
testVector2.clear();
clock_t end2 = clock();
cout << "Time 2:" << end2 - end1 << endl;
FillVector2(testVector3);
clock_t end3 = clock();
cout << "Time 3:" << end3 - end2 << endl;
std::sort(testVector3.begin(), testVector3.end(), compare2);
clock_t end4 = clock();
cout << "Time 4:" << end4 - end3 << endl;
std::set<BigTestStruct, myComp> setData;
//FillSet1(setData);
clock_t end5 = clock();
cout << "Time 5:" << end5 - end4 << endl;
//clock_t end5 = clock();
vector<BigTestStruct*> testVector6;
testVector6.reserve(COUNT);
for (int i = 0; i < COUNT; i++)
{
BigTestStruct* bt = new BigTestStruct();
bt->lValue = nnn2--;
testVector6.emplace_back(bt);
}
for (auto iter = testVector6.begin(); iter != testVector6.end(); ++iter)
{
if (*iter != nullptr)
{
delete (*iter);
(*iter) = nullptr;
}
}
testVector6.clear();
clock_t end6 = clock();
cout << "Time 6:" << end6 - end5 << endl;
//clock_t end6 = clock();
std::vector<std::shared_ptr<BigTestStruct>> ptrVec;
ptrVec.reserve(COUNT);
for (int i = 0; i < COUNT; i++)
{
BigTestStruct* bt = new BigTestStruct();
bt->lValue = nnn2--;
std::shared_ptr<BigTestStruct> aaa(bt);
/*std::shared_ptr<BigTestStruct> aaa = std::make_shared<BigTestStruct>();
aaa->lValue = nnn2--;*/
ptrVec.emplace_back(aaa);
}
ptrVec.clear();
clock_t end7 = clock();
cout << "Time 7:" << end7 - end6 << endl;
return 0;
}