c++11标准库的regex比boost库的regex之间的性能差距接近5倍,这是为何?stackflow上也找到一篇post《c++11 regex slower than python》,你们在7年前就有讨论了,可是没有一个答案。里面有人给出boost快5倍的例子。python
今天就此作一个小小的profile 进行分析对比。c++
环境:devtoolset-7 on atlarch centos 7,git
编译连接:O2优化,boost169 on dnf。github
测试内容:100次 regex_search,由于不是benchmark,因此少许100次。测试代码放在结尾。centos
量化单位:self 栏指令周期,call栏次数。网络
如今开始分析profile,每张图上面是boost::regex-profile,下面是std::regex-profile。dom
上图,ld.so能够看做一个进程的基本开销,两者是至关的,因此除去。post
测试程序名称是zbuffer,即@MAIN主映像,regex代码内联在这个模块。性能
主要模块的profile对比测试
模块名 | boost | std |
@MAIN | 329608 | 3966488 |
libstdc++ | 577719 | 1527035 |
libc | 137741 | 913459 |
libboost_regex | 117581 | 57632 |
主要差距存在前俩, boost约90w,std约450w,接近5倍。
能够直观看出,boost库在@MAIN内联的代码,加上库的执行代码,性能消耗十分少,不到50w,比std::regex程序在libc上消耗的还少的多。std::regex究竟作什么了。
第二张图是主模块的profile对比,std::regex主要消耗在前8项,主要有_Execute,new,delete。
std::regex的_Execute占了120W,new+delete占了180W。
抛开new+delete操做的巨大损耗,std::regex的_Execute的消耗依旧比boost::regex所有消耗高出1倍。
第三张图是libstdc++的profile对比,
boost基本没多少new的操做,std::regex却又大量动态类型操做,损耗50w。这损耗量,boost::regex已经足够完成工做了。
第四张图是libboost_regex的profile。
由于,std::regex测试程序也连接了libboost_regex.so,正好能够观察std::regex又在什么破事情是损耗。boost在这个模块上的消耗时实实在在的,10w+。std::regex测试程序在libstdc++库的std::string::_S_construct被libboost_regex库接替,反过来看,std::regex的std::string多了3400+次的操做。
最后一张图是libc的profile
boost::regex与std::regex的profile对比完结。
测试代码来自网络
int veces = 100; int count = 0; zlab_allocator<>::inst(); using namespace std; regex expres ("([^-]*)-([^-]*)-(\\d\\d\\d:\\d\\d)---(.*)\\n", regex_constants::ECMAScript); std::string text ("some-random-text-453:10--- etc etc blah\n"); smatch macha; auto start = std::chrono::system_clock::now(); for (int ii = 0; ii < veces; ii ++) count += regex_search (text, macha, expres); auto milli = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count(); std::cout << count << "/" << veces << " matches " << milli << " ms --> " << (milli / (float) veces) << " ms per regex_search" << std::endl;
profile文件放在个人github.com/bbqz007。