某次正则表达式的效率测试

试了下正则表达式和字符串函数的效率,例如去掉字符串里面的所有的空格:

#include <regex>  
#include <string>
#include <iostream>
#include <time.h>
#include <fstream>
using namespace std;
int main()
{
    ifstream in;
    in.open("test.txt", ios::binary);
    string str = "";
    clock_t start, ends;
    start = clock();
    while (getline(in, str))
    {
        //while (str.find(" ") != string::npos)
        //{    
        //    str.erase(str.find(" "), 1);//删除空格
        //}
        str = regex_replace(str, regex(" "), "");//删除所有的空格
    }
    ends = clock();
    cout << ends - start << endl;
    in.close();
    system("pause");
    return 0;
}

text.txt的内容:

  测试结果是正则表达式更慢!

原文地址:https://www.cnblogs.com/hosseini/p/15247647.html