正则表达式的工作速度非常慢 [英] regex works very slow

查看:28
本文介绍了正则表达式的工作速度非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多小于 64 kb 的文件,我需要使用下面的代码转换它们.

I have lots of files less than 64 kb and I need to convert them with code below.

这是带有 QRegExp 的 QT 中的工作代码.

Here is working code in QT with QRegExp.

我尝试在 MSVC 中使用 regex_replace 重写它,但遇到了工作非常缓慢的问题.

I tried to rewrite it in MSVC with regex_replace and faced with the problem of very slow work.

temp.replace(QRegExp("[ ]{0,}(=)[ ]{0,}"), QString("="));
temp.replace(QRegExp("[ ]{0,}(==)[ ]{0,}"), QString("=="));
temp.replace(QRegExp("[ ]{0,}(>)[ ]{0,}"), QString(">"));
temp.replace(QRegExp("[ ]{0,}(<)[ ]{0,}"), QString("<"));
temp.replace(QRegExp("[ ]{0,}(\\&\\&)[ ]{0,}"), QString("&&"));
temp.replace(QRegExp("[ ]{0,}(\\|\\|)[ ]{0,}"), QString("||"));
temp.replace(QRegExp("[ ]{0,}(\\})[ ]{0,}"), QString("}"));
temp.replace(QRegExp("[ ]{0,}(\\{)[ ]{0,}"), QString("{"));

MSVC:

temp = regex_replace(temp, std::regex("[ ]{0,}(=)[ ]{0,}"), "=");
temp = regex_replace(temp, std::regex("[ ]{0,}(==)[ ]{0,}"), "==");
temp = regex_replace(temp, std::regex("[ ]{0,}(>)[ ]{0,}"), ">");
temp = regex_replace(temp, std::regex("[ ]{0,}(<)[ ]{0,}"),"<" );
temp = regex_replace(temp, std::regex("[ ]{0,}(\\&\\&)[ ]{0,}"),"&&" );
temp = regex_replace(temp, std::regex("[ ]{0,}(\\|\\|)[ ]{0,}"),"||" );
temp = regex_replace(temp, std::regex("[ ]{0,}(\\})[ ]{0,}"), "}");
temp = regex_replace(temp, std::regex("[ ]{0,}(\\{)[ ]{0,}"),"{" );

例如:std::string temp(size of ~18kb) 每行处理大约 10 秒.

For example: std::string temp(size of ~18kb) processed for about 10 sec for every line.

这里可能有什么问题?

推荐答案

可用的 std::regex 实现还不是真正的抢手(还没有?),但是 boost::regex 相当快.

The std::regex implementations available aren't really up to snatch (yet?), but boost::regex is reasonably fast.

这篇关于正则表达式的工作速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆