下面是驗證代碼,往vector裏面填入數據,實驗後發現幾個現象:
1. vector插入大量數據時,用reserve比不用效率高
2. 用vector先填入數據再sort比直接用set效率要高
3.vector裏面存結構體指針,內存用的少,大數據量時建議用
4 vector 裏面用智能指針,不用考慮內存釋放,std::shared_ptr<BigTestStruct>,但是用時較多
// 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;
}