【发布时间】:2015-10-06 00:30:21
【问题描述】:
我在 VS 中运行一个 c++ 程序。我提供了一个正则表达式,我正在解析一个超过 200 万行的文件,用于匹配该正则表达式的字符串。代码如下:
int main() {
ifstream myfile("file.log");
if (myfile.is_open())
{
int order_count = 0;
regex pat(R"(.*(SOME)(\s)*(TEXT).*)");
for (string line; getline(myfile, line);)
{
smatch matches;
if (regex_search(line, matches, pat)) {
order_count++;
}
}
myfile.close();
cout << order_count;
}
return 0;
}
文件应该搜索匹配的字符串并计算它们的出现次数。我有一个 python 版本的程序,它使用相同的正则表达式在 4 秒内完成此操作。我已经等了大约 5 分钟,上面的 c++ 代码才能工作,但它仍然没有完成。它没有进入无限循环,因为我让它以一定的间隔打印出它的当前行号并且它正在进步。我应该用不同的方式编写上面的代码吗?
编辑:这是在发布模式下运行的。
编辑:这是python代码:
class PythonLogParser:
def __init__(self, filename):
self.filename = filename
def open_file(self):
f = open(self.filename)
return f
def count_stuff(self):
f = self.open_file()
order_pattern = re.compile(r'(.*(SOME)(\s)*(TEXT).*)')
order_count = 0
for line in f:
if order_pattern.match(line) != None:
order_count+=1 # = order_count + 1
print 'Number of Orders (\'ORDER\'): {0}\n'.format(order_count)
f.close()
程序终于停止运行。最令人不安的是输出不正确(我知道正确的值应该是什么)。
也许对这个问题使用正则表达式并不是最好的解决方案。如果我找到更好的解决方案,我会更新。
编辑:根据@ecatmur 的回答,我做了以下更改,c++ 程序运行得更快了。
int main() {
ifstream myfile("file.log");
if (myfile.is_open())
{
int order_count = 0;
regex pat(R"(.*(SOME)(\s)*(TEXT).*)");
for (string line; getline(myfile, line);)
{
if (regex_match(line, pat)) {
order_count++;
}
}
myfile.close();
cout << order_count;
}
return 0;
}
【问题讨论】:
-
你在调试模式下运行它吗?
-
也添加你的python版本。请。你在同一台机器上比较吗? 防病毒软件是否已关闭?
-
确保您在此处阅读答案:stackoverflow.com/questions/14205096/…
-
我猜你的 python 版本正在执行 one 读取(在
for line in f它会读取整个文件),而你的 C++ 程序正在执行 ~ 2M 读取(每行一个)。从/向磁盘读取/写入通常很慢,所以我想最好将整个文件内容读取一次,然后逐行解析相应的字符串。 -
您可以通过每次读取一行来测试 C++ 程序变慢的理论:将
regex_search替换为仅增加order_count的行,并查看是否该程序仍然需要很长时间才能运行。