大数据量下vector的存数据效率,排序

本文通过实验对比了在C++中使用vector容器的不同操作方法,包括数据填充、预分配内存、排序以及存储结构体指针和智能指针的性能差异。发现使用赋值方式插入数据、预分配内存、先填充后排序以及存储结构体指针能显著提升效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是验证代码,往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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值