【发布时间】:2023-04-05 01:28:02
【问题描述】:
我正在尝试读取 ~36KB,完成此循环需要 ~20 秒:
ifstream input_file;
input_file.open("text.txt");
if( !(input_file.is_open()) )
{
cout<<"File not found";
exit(1);
}
std::string line;
stringstream line_stream; //to use << operator to get words from lines
int lineNum=1;
while( getline(input_file,line) ) //Read file line by line until file ends
{
line_stream.clear(); //clear stream
line_stream << line; //read line
while(line_stream >> word) //Read the line word by word until the line ends
{
//insert word into a linked list...
}
lineNum++;
}
input_file.close();
任何帮助将不胜感激。
【问题讨论】:
-
事实上,问题可能是您插入到链表中。它可能是 O(n^2),这取决于它是如何实现的。而对于 36kB,“
n”可能会很大。 -
你是对的!我评论了插入部分,代码暂时结束了……我现在就去找问题。谢谢:)