我是新来的精神和一般的提升。我试图解析类似如下的VRML文件的一部分:提升精神:缓慢解析优化
point
[
#coordinates written in meters.
-3.425386e-001 -1.681608e-001 0.000000e+000,
-3.425386e-001 -1.642545e-001 0.000000e+000,
-3.425386e-001 -1.603483e-001 0.000000e+000,
注释与#是可选的开始。
我写了一个语法,工作正常,但解析过程花了很长时间。我想优化它以更快地运行。我的代码如下所示:
struct Point
{
double a;
double b;
double c;
Point() : a(0.0), b(0.0), c(0.0){}
};
BOOST_FUSION_ADAPT_STRUCT
(
Point,
(double, a)
(double, b)
(double, c)
)
namespace qi = boost::spirit::qi;
namespace repo = boost::spirit::repository;
template <typename Iterator>
struct PointParser :
public qi::grammar<Iterator, std::vector<Point>(), qi::space_type>
{
PointParser() : PointParser::base_type(start, "PointGrammar")
{
singlePoint = qi::double_>>qi::double_>>qi::double_>>*qi::lit(",");
comment = qi::lit("#")>>*(qi::char_("a-zA-Z.") - qi::eol);
prefix = repo::seek[qi::lexeme[qi::skip[qi::lit("point")>>qi::lit("[")>>*comment]]];
start %= prefix>>qi::repeat[singlePoint];
//BOOST_SPIRIT_DEBUG_NODES((prefix)(comment)(singlePoint)(start));
}
qi::rule<Iterator, Point(), qi::space_type> singlePoint;
qi::rule<Iterator, qi::space_type> comment;
qi::rule<Iterator, qi::space_type> prefix;
qi::rule<Iterator, std::vector<Point>(), qi::space_type> start;
};
,我打算解析部分,位于输入文本的中间,所以我需要跳过的文字部分,以得到它。我使用repo :: seek来实现它。这是最好的方法吗?
我以下列方式运行解析器:
std::vector<Point> points;
typedef PointParser<std::string::const_iterator> pointParser;
pointParser g2;
auto start = ch::high_resolution_clock::now();
bool r = phrase_parse(Data.begin(), Data.end(), g2, qi::space, points);
auto end = ch::high_resolution_clock::now();
auto duration = ch::duration_cast<boost::chrono::milliseconds>(end - start).count();
要在输入文本解析约80K项,大约需要2.5秒,这是我的需求相当缓慢。我的问题是有没有办法以更优化的方式编写解析规则以使其更快(更快)?我怎样才能改进这个实现呢?
我是新来的精神,所以一些解释将不胜感激。
如果相同的代码36毫秒和2.5秒的类似系统(球场)和类似的输入运行时间之间获得,我的办法是使后者没有优化上。 – rubenvb
对此不确定。我使用VS2013的预编译版本的boost 1.59库。你能建议任何优化标志吗? –
Boost(.Spirit)非常模板化,预编译的库不会让你放慢速度......你为什么认为*你的代码编译得太慢了?精神是在你使用它的时候编译的,而不是在前面。你需要确保你的代码的定时发布版本。 – rubenvb