使用c ++减少两个映射的处理时间

时间:2016-05-03 22:53:58

标签: c++

我有两张地图:Pkt_creatingmap和Pkt_arrivalmap。 (Pkt_creatingmap)映射中的元素数远远多于(Pkt_arrivalmap)映射中的元素数[即:Pkt_creatingmap中的元素总数约650000个元素和Pkt_arrivalmap中元素的总数约72000个元素]。 我想比较(Pkt_creatingmap)地图和(Pkt_arrivalmap)地图中的关键ID(第一)。如果它相等,则减去密钥ID的值(秒)并将结果存储在列表中(TotalTrafficETED)。 我写下面的代码来完成这项任务,但是需要很长时间才能完成(即:这很耗时)。并非此过程中需要(Pkt_creatingmap)地图中的所有元素。减法过程仅取决于(Pkt_arrivalmap)地图的元素。 有减少运行时间的方法吗? (即:(Pkt_creatingmap)映射的迭代器可以在(Pkt_arrivalmap)映射迭代器的结束位置结束吗? 希望我的问题很明确。 任何人都可以帮助我,或者给我另一个完成这项任务的理想..

#include <iostream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <sstream>
#include <cmath>
#include <map>
#include <iterator>
#include <list>
using namespace std;

int main()
{
    map<double_t, double_t> Pkt_arrivalmap;
    map<double_t, double_t> Pkt_creatingmap;
    list<double_t> TotalTrafficETED;
    int pos, pos2, pos3;
    double_t ArrivingID, CreatingID, ArrivingTime, CreatingTime, Pkt_ETED, TTETED;
    string line, search, word1 = "ns", word2 = "Pkt_create", word3 = "Pkt_recv";
    ifstream inf;
    inf.open("123.txt");
    if (inf.is_open()) {

        while (getline(inf, line)) {

            if (line.find("Pkt_recv") != string::npos) {
                pos = line.find(word1);
                pos3 = line.find(word3);
                line.replace(pos, word1.length(), "  "); // to remove "ns"
                line.replace(pos3, word3.length(), "  "); // to remove "Pkt_recv"
                istringstream iss(line);
                double_t column1;
                //double_t column3;
                double_t column2;
                iss >> column1 >> column2;
                //cout << column1 <<"  "<< column2 <<"  "<< " "<< endl;
                Pkt_arrivalmap[column1] = column2;
            }
        }

        inf.close();
    }

    else
        cout << "Unable to open file inf \n";
    for (std::map<double_t, double_t>::iterator it = Pkt_arrivalmap.begin(); it != Pkt_arrivalmap.end(); it++) {
        cout << "Arrival info."
             << "  " << it->first << "  " << it->second << "  " << Pkt_arrivalmap.size() << endl;
    }

    inf.open("123.txt");
    if (inf.is_open()) {

        while (getline(inf, line)) {

            if (line.find("Pkt_create") != string::npos) {
                pos = line.find(word1);
                pos2 = line.find(word2);
                line.replace(pos, word1.length(), "  "); // to remove "ns"
                line.replace(pos2, word2.length(), "  "); // to remove "Pkt_create"
                istringstream iss(line);
                double_t column1;
                //double_t column3;
                double_t column2;
                iss >> column1 >> column2;
                //cout << column1 <<"  "<< column2 <<"  "<< " "<< endl;
                Pkt_creatingmap[column1] = column2;
            }
        }

        inf.close();
    }

    else
        cout << "Unable to open file inf \n";
    for (std::map<double_t, double_t>::iterator it = Pkt_creatingmap.begin(); it != Pkt_creatingmap.end(); it++) {
        cout << "Creating info."
             << "  " << it->first << "  " << it->second << "  " << Pkt_creatingmap.size() << endl;
    }

    for (std::map<double_t, double_t>::iterator it = Pkt_arrivalmap.begin(); it != Pkt_arrivalmap.end(); it++) {

        ArrivingID = it->first;
        ArrivingTime = it->second;
        //cout << "Ahmed" <<"  "<< ArrivingID <<endl;
        for (std::map<double_t, double_t>::iterator it = Pkt_creatingmap.begin(); it != Pkt_creatingmap.end(); it++) {
            CreatingID = it->first;
            CreatingTime = it->second;
            if (ArrivingID == CreatingID) {
                Pkt_ETED = (ArrivingTime - CreatingTime) / 1000000000;
                TotalTrafficETED.push_back(Pkt_ETED);
                Pkt_creatingmap.erase(it->first);
                Pkt_arrivalmap.erase(it->first);
                cout << "Pkt_ID:" << CreatingID << ","
                     << "Pkt_ETED:" << Pkt_ETED << endl;
            }
        }
    }
    for (std::list<double_t>::iterator it = TotalTrafficETED.begin(); it != TotalTrafficETED.end(); it++) {
        TTETED += *it / TotalTrafficETED.size();
    }
    cout << "Total Traffic ETED"
         << "  " << TTETED << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果要在地图中查找具有给定ID的元素,方法map::find非常有效,并且在地图大小上以对数方式进行,因为它是一个已排序的容器。在你的代码中,你做两个for循环,复杂性是二次的。因此,你必须写出类似的东西:

for (std::map<double_t,double_t>::iterator it = Pkt_arrivalmap.begin(); it != Pkt_arrivalmap.end(); it++)
    { 

     ArrivingID =it->first;
     ArrivingTime=it->second;
     //cout << "Ahmed" <<"  "<< ArrivingID <<endl;

     map<double_t,double_t>::iterator it2 = Pkt_creatingmap.find(ArrivingID);
     if (it2!=Pkt_creatingmap.end()){ // Found equivalent key

        CreatingTime=it2->second;
        Pkt_ETED=(ArrivingTime-CreatingTime)/1000000000;
        TotalTrafficETED.push_back(Pkt_ETED);
        Pkt_creatingmap.erase(it->first);
        Pkt_arrivalmap.erase(it2->first);
        cout <<"Pkt_ID:"<<CreatingID<<","<<"Pkt_ETED:"<<Pkt_ETED<<endl;
      }
    }