【发布时间】:2016-05-07 16:58:18
【问题描述】:
我有以下代码 sn-p 从std::cin 读取行并将它们打印到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;
}
对于 9800 行的输入,执行时间非常快:
real 0m0.116s
user 0m0.056s
sys 0m0.024s
但是,如果我在 while 循环中取消注释 std::regex e1 对象,执行时间会大大减慢:
real 0m2.859s
user 0m2.800s
sys 0m0.032s
另一方面,在循环外取消注释std::regex e2 对象,执行时间完全不受影响。考虑到我没有应用任何正则表达式匹配,但我只是在构造一个对象,为什么会发生这种情况?
注意:我已经看到 this 线程但没有发现任何问题。
【问题讨论】: