I have the following code snippet that reads lines from std::cin and prints them to std::cout.
#include <iostream>
#include <string>
#include <regex>
int main() {
//std::regex e2("([^[:blank:]]+)|(\"[^\"]+\")|(\\([^\\)]+\\))");
const size_t BUFSIZE = (1<<10);
std::string buffer;
buffer.reserve( BUFSIZE );
while (std::getline( std::cin, buffer )) {
std::cout << buffer << std::endl;
//std::regex e1("([^[:blank:]]+)|(\"[^\"]+\")|(\\([^\\)]+\\))");
}
return 0;
}
The execution time is quite fast for an input of 9,800 lines:
real 0m0.116s
user 0m0.056s
sys 0m0.024s
However, if I uncomment the std::regex e1 object in the while loop, the execution time is slowed down considerably:
real 0m2.859s
user 0m2.800s
sys 0m0.032s
On the other hand, uncommenting the std::regex e2 object, outside the loop, the execution time is not affected at all. Why is this happening, considering that I am not applying any regex matches, but I'm only constructing an object?
NB: I've seen this thread but didn't shed any light.