【问题标题】:Replacing these methods with faster ones用更快的方法替换这些方法
【发布时间】:2013-04-13 17:26:51
【问题描述】:

这个问题实际上是我发布的a previous question 的更新。根据用户的意见,我意识到我需要分析我的代码,因此我通过 Vtune Amp 分析了我的代码的第一部分。与其他方法相比,我得到了以下语句,这些语句消耗了大量时间

Source Line Source                                                                        CPU Time by Utilization   Overhead Time   Spin Time
double high_val =  atof(temp[2].c_str());                                                                           
std::string s( (std::istreambuf_iterator<char>(&buffer)), std::istreambuf_iterator<char>());            
boost::split( temp, lines[i], boost::is_any_of(",") );                                              

在上面的代码缓冲区中是: boost::asio::streambuf buffer;

对上述替换功能有什么建议吗?

【问题讨论】:

  • buffer 是什么类型?
  • 好的,我明白了。你打算用s做什么?真的需要创建这个对象,或者您可以直接在buffer.data() 上工作?
  • 我将缓冲区复制到字符串的唯一原因是因为我想将此字符串传递给下一条语句boost::split()
  • 所以实际上是temp,而不是s
  • 一旦字符串被发送到各种不同的方法。这些方法使用 boost::split 进一步拆分此字符串,并将字符串拆分成的向量是lines

标签: c++ boost


【解决方案1】:

尝试Boost.Sprit 完成所有解析任务。如果你有很多规则,它可能会消耗更多的编译时资源,但在运行时会很快。

第一行:

#include <boost/spirit/include/qi.hpp>
std::string::iterator begin = temp.begin() + 2;
std::string::iterator end = input.end();
float high_val;
boost::spirit::qi::parse(begin, end, boost::spirit::float_, high_val);

最后一行:

std::vector<std::string>;
// '%' is a list parser
boost::spirit::qi::parse(buffer.data(), buffer.data() + buffer.size(), *(char_ - ',') % ',', lines);

很可能,可以为你的所有任务创建一个简单的语法,但我不知道你在解析什么,所以我只是尝试或多或少地匹配你上面的代码。

【讨论】:

  • 我将如何在提振精神中做这样的事情boost::algorithm::split_regex( Vector, string, boost::regex( "\r\n" ) ) ;
  • 栈向量是不是叫lines
  • @Rajeshwar 如上所示,您可以使用 % parser 拆分任何内容。当然,您必须学习 Spirit(至少它的 Qi 部分 - 用于解析)才能有效地使用它(boost.org/doc/libs/1_53_0/libs/spirit/doc/html/spirit/qi.html)。
  • @Rajeshwar 您的输入格式究竟是什么,您需要如何摆脱它?
  • 感谢您的回复和指导。我目前正在阅读精神文档。如果我有任何问题,我会在这里发布问题。似乎精神也可以解决我的很多问题。再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 2022-07-27
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多