【问题标题】:Strings and unordered_map running slow字符串和 unordered_map 运行缓慢
【发布时间】:2013-03-05 22:54:52
【问题描述】:

这是我的代码中运行非常慢的 2 个函数。 基本上我读入文档名称,打开文档,然后一次处理一个单词。我需要将文档拆分成句子,并给每个句子一个哈希表,表示单词在句子中出现的次数。我还需要跟踪所有新词,以及整个文档的哈希表。

当我现在在 10 个文档上运行我的代码时,这些文档总共有 8000 个单词和 2100 个 uniq 单词,运行大约需要 8000 多秒...每个单词几乎 1 秒。

你能告诉我if(istream.good()) 需要多长时间吗?

或者,如果您能知道什么时候延迟了我的代码。如果部分不清楚,请告诉我,我会提供帮助。

附:您可以在代码中看到我有一个start = clock()end = clock() 评论它不断返回

void  DocProcess::indexString(string sentenceString, hash * sent){

stringstream iss;

string word;
iss.clear();
iss << sentenceString;

while(iss.good())
{

    iss >> word;
    word = formatWord(word);

    std::unordered_map<std::string,int>::const_iterator IsNewWord = words.find(word);

    if(IsNewWord == words.end())
    {
        std::pair<std::string,int> newWordPair (word,0);
        std::pair<std::string,int> newWordPairPlusOne (word,1);

        words.insert(newWordPair);
        sent->insert(newWordPairPlusOne);
    }
    else
    {
        std::pair<std::string,int> newWordPairPlusOne (word,1);
        sent->insert(newWordPairPlusOne);
    }
}

} 无效 DocProcess::indexFile(string iFileName){

hash newDocHash;
hash newSentHash;
scoreAndInfo sentenceScore;
scoreAndInfo dummy;

fstream iFile;
fstream dFile;
string word;
string newDoc;
string fullDoc;
int minSentenceLength = 5;
int docNumber = 1;
int runningLength = 0;
int ProcessedWords = 0;
stringstream iss;

iFile.open(iFileName.c_str());

if(iFile.is_open())
{
    while(iFile.good())
    {
        iFile >> newDoc;
        dFile.open(newDoc.c_str());
        DocNames.push_back(newDoc);

        if(dFile.is_open())
        {
            scoreAndInfo documentScore;
            //iss << dFile.rdbuf();
            while(dFile.good())
            {
                //start = clock();
                dFile >> word;
                ++ProcessedWords;

                std::unordered_map<std::string,int>::const_iterator IsStopWord = stopWords.find(word);


                if(runningLength >= minSentenceLength && IsStopWord != stopWords.end() || word[word.length()-1] == '.')
                {

                    /* word is in the stop list, process the string*/
                    documentScore.second.second.append(" "+word);
                    sentenceScore.second.second.append(" "+word);

                    indexString(sentenceScore.second.second, &sentenceScore.second.first);

                    sentenceScore.first=0.0;
                    SentList.push_back(sentenceScore);
                    sentenceScore.second.first.clear(); //Clear hash
                    sentenceScore.second.second.clear(); // clear string
                    //sentenceScore = dummy;
                    runningLength = 0;
                }
                else
                {
                    ++runningLength;
                    sentenceScore.second.second.append(" "+word);
                    documentScore.second.second.append(" "+word);

                }
                //end = clock();
                    system("cls");
                    cout    << "Processing doc number: " << docNumber << endl
                        << "New Word count: " << words.size() << endl
                        << "Total words: " << ProcessedWords << endl;
                        //<< "Last process time****: " << double(diffclock(end,start)) << " ms"<< endl;

            }
            indexString(documentScore.second.second, &documentScore.second.first);
            documentScore.first=0.0;
            DocList.push_back(documentScore);
            dFile.close();
            //iss.clear();
            //documentScore = dummy;
            ++docNumber;
            //end = clock();
            system("cls");
            cout    << "Processing doc number: " << docNumber << endl
                << "Word count: " << words.size();
                //<< "Last process time: " << double(diffclock(end,start)) << " ms"<< endl;

        }
    }

    iFile.close();
}
else{ cout << "Unable to open index file: "<<endl <<iFileName << endl;}

} `

【问题讨论】:

  • 你可以用while (iss &gt;&gt; word)代替while(iss.good())
  • 尝试为您的哈希指定一个初始大小,这样它就不必调整大小 - 如果您期望大约 8000 个唯一条目,请创建大约 10,000 个存储桶。另外,尝试将散列切换到普通的 std::map,看看你的性能是否显着提高。如果是这样,问题在于您对哈希表的使用。如果不是,它在其他地方。
  • 我想我应该让你知道的。哈希定义为#define hash std::unordered_map&lt;std::string, int&gt;

标签: c++ stl unordered-map istream


【解决方案1】:

你可以试试吗

                system("cls");

在任何循环中?这肯定没有帮助,这是一个昂贵的电话。

【讨论】:

  • 我同时尝试了所有的cmets,有些东西解决了这个问题。不是 100% 确定它是什么,但它现在运行得更快了!! WOOT
  • 只需通过重新输入系统调用来检查,这肯定是问题所在。有没有办法快速清除cmd?
【解决方案2】:

要快速清除屏幕,请尝试cout &lt;&lt; '\f';,而不是system("cls");

【讨论】:

  • 我发现最好的办法是用 printf() 和 .\r 替换
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
相关资源
最近更新 更多