How can the small change in the command string can cause such a large
performance difference. Is my regex wrong?
The intent is to extract the (possibly multiline string) between the two markers '|>'.
I compiled this code using Visual Studio and Microsoft C++ compiler.
bool testCommandMatching() {
const std::string group(R"(
gcc
src\hello.c
-o bin\hello
)");
const std::regex cmdRe(R"(^\|>((?:.*\s*)*)\|>)");
bool bothMatched = true;
{
std::cmatch match;
const std::string command(R"(|>
gcc
src\hello.c
-o bin\hello
|>)");
auto start = std::chrono::system_clock::now();
bool matched = std::regex_search(command.c_str(), match, cmdRe, std::regex_constants::match_continuous);
auto end = std::chrono::system_clock::now();
// elapsed is less than 10 ms
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
bothMatched = bothMatched && group == (matched ? match[1] : std::string(""));
}
{
std::cmatch match;
const std::string command(R"(|>
gcc
src\hello.c
-o bin\hello
|> bin\hello)");
auto start = std::chrono::system_clock::now();
bool matched = std::regex_search(command.c_str(), match, cmdRe, std::regex_constants::match_continuous);
auto end = std::chrono::system_clock::now();
// elapsed is more than 1000 ms. How is this possible?
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
bothMatched = bothMatched && group == (matched ? match[1] : std::string(""));
}
return bothMatched;
}