相关疑难解决方法(0)

c ++ 11正则表达式比python慢

嗨,我想了解为什么以下代码使用正则表达式进行拆分字符串拆分

#include<regex>
#include<vector>
#include<string>

std::vector<std::string> split(const std::string &s){
    static const std::regex rsplit(" +");
    auto rit = std::sregex_token_iterator(s.begin(), s.end(), rsplit, -1);
    auto rend = std::sregex_token_iterator();
    auto res = std::vector<std::string>(rit, rend);
    return res;
}

int main(){
    for(auto i=0; i< 10000; ++i)
       split("a b c", " ");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

比下面的python代码慢

import re
for i in range(10000):
    re.split(' +', 'a b c')
Run Code Online (Sandbox Code Playgroud)

这里的

> python test.py  0.05s user 0.01s system 94% cpu 0.070 total
./test  0.26s user 0.00s system 99% cpu 0.296 total …
Run Code Online (Sandbox Code Playgroud)

c++ python regex performance c++11

72
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

performance ×1

python ×1

regex ×1