问题
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