내 코드에서 REALLY SLOW가 실행되는 두 가지 함수가 있습니다. 기본적으로 문서 이름을 읽고 문서를 연 다음 한 번에 한 단어 씩 처리합니다. 나는 문서를 문장들로 나누고 각 문장에 단어가 문장 안에 나타나는 횟수를 나타내는 해쉬 테이블을 줄 필요가있다. 또한 모든 새 단어를 추적하고 전체 문서의 해시 테이블을 유지해야합니다.문자열과 unordered_map이 느리게 실행 중임
지금은 10 개의 문서에서 코드를 실행하면 합계 8000 단어와 2100 유니크 단어가 실행되는데 약 8000 초가 걸립니다 ... 단어 당 거의 1 초.
if(istream.good())
에 걸릴 시간을 알려주시겠습니까?
또는 내 코드가 지연 될 때를 알 수 있다면. 섹션이 명확하지 않으면 알려주십시오. 제가 도와 드리겠습니다.
P. 당신은 코드에서 내가 어디 start = clock()
및 end = clock()
지속적으로 대답을 < 1ms 반환 댓글을 볼 수 있습니다. 그리고 그 마음 boggleing
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 (문자열 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.good()) '대신'while (iss >> word)'만 할 수 있습니다. –
크기를 변경할 필요가 없도록 해시의 초기 크기를 지정하십시오. ~ 8000 개의 고유 항목이 예상되므로 10,000 개의 버킷을 만들 수 있습니다. 또한 해시를 정상 std :: map으로 전환하여 성능이 크게 향상되는지 확인하십시오. 그렇다면 해시 테이블을 사용하는 데 문제가 있습니다. 그렇지 않은 경우 다른 곳에서 발생합니다. – Rollie
나는 너에게 알려 줘야한다고 생각해. 해쉬는'#define hash std :: unordered_map'으로 정의됩니다. –
KevinCameron1337