如何解决1000万条数据(key,value)插入到地图容器中作统计时间慢的有关问题
怎么解决1000万条数据(key,value)插入到map容器中作统计时间慢的问题
------解决方案--------------------
------解决方案--------------------
用python写了个,发现的确很慢
------解决方案--------------------
#include<iostream>
#include<unordered_map>
#include<vector>
#include<windows.h>
#include<ctime>
#include<algorithm>
using namespace std;
const int MAX=1500;
const int MAXN=5000000;
int main()
{
cout<<"测试1000个设备,每个设备有10000条数据用map统计每个时间点对应哪些设备及相应的数据…………"<<endl;
typedef pair<long long, float> time_data_t;//时间--数据
typedef pair<int, float> device_data_t;//设备号--数据
typedef vector<long long> nset_v;//时间节点
typedef vector<time_data_t> time_data_v;
typedef unordered_map<long long, vector<device_data_t>> time_device_t;//时间--设备
//产生1000个时间间隔,保存在set中
srand(time(NULL));
nset_v nset(1000);
for(auto &&a: nset) a = (rand()*rand())%MAX;
//排序去重
sort(nset.begin(), nset.end());
nset.erase(unique(nset.begin(), nset.end()), nset.end());
cout<<"开始生成数据……"<<endl;
DWORD stime = GetTickCount();
time_data_v map1(10000);//预先分配内存
time_device_t nmap;
for(int i = 0, finali = nset.size(); i != finali; ++i){
map1.clear();//清空数据,并不释放内存
for(int j=0;j<10000;j++) map1.emplace_back(nset[i]*j/*时间点*/, (rand()*rand())%MAXN/*数据,为什么产生的整数*/);
for(auto &&tmp: map1) nmap[tmp.first].emplace_back(i+1,tmp.second);
}
DWORD etime = GetTickCount();
cout<<"10000000条数据用unordered_map统计共花费"<<etime-stime<<"毫秒"<<endl;
}
------解决方案--------------------
用python写了个,发现的确很慢
#时间点map
#时间点:[<设备索引,数据>....]
def foo():
timeDict = dict()
oldt = time.time()
maxInt = 256**4-1
per100times = oldt
for x in range(1000):
#模拟每台机器的时间点
period = int(1+(random.randint(0,maxInt)*random.randint(0,maxInt))%1500)
#时间点
for time1 in range(period,period*10001,period):
#模拟生成一个数据并插入
l = timeDict.get(time1)
if not l:
l = []
timeDict[time1] = l
l.append((x,time1))#random.randint(0,maxInt)%5000000