我认为 boost 正则表达式引擎会比 boost::algorithm 更快
这个简单的测试显示 algo 大大击败了正则表达式引擎
这是整个测试程序
我错过了什么吗?
#include "boost/algorithm/string.hpp"
#include "boost/regex.hpp"
#include "boost/xpressive/xpressive.hpp"
#include "boost/progress.hpp"
#include <iostream>
int main()
{
boost::timer tm;
const int ITERATIONS = 10000000;
{
std::string input("This is his face");
tm.restart();
for( int i = 0; i < ITERATIONS; ++i)
{
boost::algorithm::replace_all(input,"his","her");
}
std::cout << "boost::algorithm: " << tm.elapsed()/60 << std::endl;
}
{
std::string input("This is his face");
boost::regex expr("his");
std::string format("her");
tm.restart();
for( int i = 0; i < ITERATIONS; ++i)
{
boost::regex_replace( input, expr, format );
}
std::cout << "boost::regex: " << tm.elapsed()/60 << std::endl;
}
{
std::string input("This is his face");
boost::xpressive::sregex expr = boost::xpressive::as_xpr("his");
std::string format("her");
tm.restart();
for( int i = 0; i < ITERATIONS; ++i)
{
boost::xpressive::regex_replace(input, expr, format);
}
std::cout << "boost::xpressive: " << tm.elapsed()/60 << std::endl;
}
return 0;
}