regex works very slow

风格不统一 提交于 2020-01-16 03:24:47

问题


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

Here is working code in QT with QRegExp.

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

QT:

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,}"),"{" );

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

What is possibly wrong here?


回答1:


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



来源:https://stackoverflow.com/questions/33163365/regex-works-very-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!