c++ - boost::spirit::qi 性能

标签 c++ boost boost-spirit boost-spirit-qi

我有以下片段。

#include <iostream>
#include <sstream>
#include <chrono>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/classic.hpp>

namespace qi = boost::spirit::qi;
namespace classic = boost::spirit::classic;

template<typename T>
void output_time(const T& end, const T& begin)
{
   std::cout << std::chrono::duration_cast<std::chrono::seconds>(
         end - begin).count() << std::endl;
}

template<typename Iter>
struct qi_grammar : public qi::grammar<Iter>
{
   qi_grammar():qi_grammar::base_type(rule_)
   {
      rule_ = *string_;
      string_ = qi::char_('"') >> *(qi::char_ - '"') >> qi::char_('"');
   }
   qi::rule<Iter> rule_;
   qi::rule<Iter> string_;
};

template<typename Iter>
struct classic_grammar : public classic::grammar<classic_grammar<Iter>>
{
   template<typename ScannerT>
   struct definition
   {
      definition(const classic_grammar&)
      {
         rule = *string_;
         string_ = classic::ch_p('"') >> *(classic::anychar_p - '"') >> classic::ch_p('"');
      }
      classic::rule<ScannerT> rule, string_;
      const classic::rule<ScannerT>& start() const { return rule; }
   };
};

template<typename Iter>
void parse(Iter first, Iter last, const qi_grammar<Iter>& prs)
{
   auto start = std::chrono::system_clock::now();
   for (int i = 0; i < 100; ++i)
   {
      Iter next = first;
      if (!qi::parse(next, last, prs) || next != last)
      {
         assert(false);
      }
   }
   auto finish = std::chrono::system_clock::now();
   output_time(finish, start);
}

template<typename Iter>
void parse_c(Iter first, Iter last, const classic_grammar<Iter>& prs)
{
   auto start = std::chrono::system_clock::now();
   for (int i = 0; i < 100; ++i)
   {
      auto info = classic::parse(first, last, prs);
      if (!info.hit) assert(false);
   }
   auto finish = std::chrono::system_clock::now();
   output_time(finish, start);
}

int main()
{
   qi_grammar<std::string::const_iterator> qi_lexeme;
   classic_grammar<std::string::const_iterator> classic_lexeme;
   std::stringstream ss;
   for (int i = 0; i < 1024 * 500; ++i)
   {
      ss << "\"name\"";
   }
   const std::string s = ss.str();
   std::cout << "Size: " << s.size() << std::endl;
   std::cout << "Qi" << std::endl;
   parse(s.begin(), s.end(), qi_lexeme);
   std::cout << "Classic" << std::endl;
   parse_c(s.begin(), s.end(), classic_lexeme);
}

结果是

forever@pterois:~/My_pro1/cpp_pro$ ./simple_j 
Size: 3072000
Qi
0
Classic
1

因此,qi 解析速度比经典解析速度快。但是当我将 string_ 规则的属性更改为 std::string() (即 qi::rule<Iter, std::string()> string_; )时,我有

forever@pterois:~/My_pro1/cpp_pro$ ./simple_j 
Size: 3072000
Qi
19
Classic
1

它非常非常慢。我做错了什么?谢谢。

编译器:gcc 4.6.3。 boost - 1.48.0。标志:-std=c++0x -O2。在 LWS 上结果相同。

使用 char_ 的语义 Action ,即

string_ = qi::char_('"') >> *(qi::char_[boost::bind(&some_f, _1)] - '"')
 >> qi::char_('"')[boost::bind(&some_clear_f, _1)];

boost 性能,但我也在寻找另一种解决方案(如果存在的话)。

最佳答案

我想我之前在 SO 上回答过一个非常相似的问题。遗憾的是,我找不到它。

简而言之,您可能更愿意在源数据中使用迭代器,而不是在每个匹配项上分配(和复制)字符串。

使用时

qi::rule<Iter, boost::iterator_range<Iter>()> string_;
string_ = qi::raw [ qi::char_('"') >> *(qi::char_ - '"') >> qi::char_('"') ];

我得到了(相当大 (16x) 的数据集):

Size: 49152000
Qi
12
Classic
11

其实把规则本身改成

之后
  string_ = qi::raw [ qi::lit('"') >> *~qi::char_('"') >> '"' ];

我得到了

Size: 49152000
Qi
7
Classic
11

所以……我想这很不错。在 LWS 上查看:http://liveworkspace.org/code/opA5s$0

For completeness, obviously you can get a string from the iterator_range by doing something like

const std::string dummy("hello world");
auto r = boost::make_iterator_range(begin(dummy), end(dummy));
std::string asstring(r.begin(), r.end());

诀窍是将实际的字符串构建延迟到需要的时候。您可能想让这个技巧自动发生。这就是 Spirit Lex 对标记属性所做的。您可能想调查一下。

关于c++ - boost::spirit::qi 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14398445/

相关文章:

c# - 在需要执行 C# 应用程序的 Ubuntu 上运行的 Node 服务器 - 如何?

linux - Linux 发行版中的 boost 版本

c++ - 如何使用 Boost.Test 对跳过的测试进行硬编码?

c++ - 如何正确使用 boost::program_options::implicit_value 作为字符串 vector ?

c++ - 关于 Boost::Spirit 自动规则行为的困惑

c++ - 使用 Boost Spirit 解析语法

c++ - Boost graphviz自定义顶点标签

c++ - OpenGL 两个不同的 3d 渲染图片控件在单个 MFC 对话框上不起作用

java - 什么是 C++1 1's equivalent of Java' s instanceof

c++ - 尝试使用 Phoenix Bind 计算字符数时出现 boost.spirit "invalid static_cast"错误