C ++:快速将映射文件读入矩阵的方法

时间:2015-02-05 09:31:14

标签: c++ memory boost

我正在尝试将映射文件读入矩阵。该文件是这样的:

name;phone;city\n
Luigi Rossi;02341567;Milan\n
Mario Bianchi;06567890;Rome\n
.... 

它很安静。我写的代码工作正常,但速度不是很快:

#include <iostream>
#include <fstream>
#include <string>
#include <boost/iostreams/device/mapped_file.hpp>

using namespace std;

int main() {

    int i;
    int j=0;
    int k=0;

    vector< vector<char> > M(10000000, vector<string>(3));

    mapped_file_source file("file.csv");

    // Check if file was successfully opened
    if(file.is_open()) {

      // Get pointer to the data
      const char * c = (const char *)file.data();

      int size=file.size();

      for(i = 0; i < (size+1); i++){

       if(c[i]=='\n' || i==size){
        j=j+1;
        k=0;
       }else if(c[i]==';'){
        k=k+1;
       }else{
        M[j][k]+=c[i];
       }    
     }//end for


   }//end if    

 return(0)


}

有更快的方法吗?我已经阅读了有关memcyp的内容,但我不知道如何使用它来加速我的代码。

1 个答案:

答案 0 :(得分:6)

我有很多这样做的例子/类似的写在SO上。

让我列出最相关的内容:

在所有其他情况下,考虑使用boost::string_ref而不是vector<char>来攻击Spirit Qi作业(除非映射文件不是&#34; const&#34;当然) 。

string_ref也显示在之前链接的最后一个答案中。另一个有趣的例子(对未转义的字符串值进行延迟转换)是How to parse mustache with Boost.Xpressive correctly?

样本

这是齐工作抨击它:

  • 它将一个994 MiB文件解析为2.9s内约3200万行,

    struct Line {
        boost::string_ref name, city;
        long id;
    };
    
  • 请注意我们解析数字,并通过引用它们在内存映射中的位置+长度(string_ref

  • 来存储字符串
  • 它可以打印10条随机行的数据
  • 如果你一次在矢量中保留32m元素,它可以以2.5s的速度运行;在这种情况下,程序只进行一次内存分配。
  • 注意:在64位系统上,如果平均行长度小于40个字节,则内存表示会大于输入大小。这是因为string_ref是16个字节。

<强> Live On Coliru

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/utility/string_ref.hpp>

namespace qi = boost::spirit::qi;
using sref   = boost::string_ref;

namespace boost { namespace spirit { namespace traits {
    template <typename It>
    struct assign_to_attribute_from_iterators<sref, It, void> {
        static void call(It f, It l, sref& attr) { attr = { f, size_t(std::distance(f,l)) }; }
    };
} } }

struct Line {
    sref name, city;
    long id;
};

BOOST_FUSION_ADAPT_STRUCT(Line, (sref,name)(long,id)(sref,city))

int main() {
    boost::iostreams::mapped_file_source mmap("input.txt");

    using namespace qi;

    std::vector<Line> parsed;
    parsed.reserve(32000000);
    if (phrase_parse(mmap.begin(), mmap.end(), 
                omit[+graph] >> eol >>
                (raw[*~char_(";\r\n")] >> ';' >> long_ >> ';' >> raw[*~char_(";\r\n")]) % eol,
                qi::blank, parsed))
    {
        std::cout << "Parsed " << parsed.size() << " lines\n";
    } else {
        std::cout << "Failed after " << parsed.size() << " lines\n";
    }

    std::cout << "Printing 10 random items:\n";
    for(int i=0; i<10; ++i) {
        auto& line = parsed[rand() % parsed.size()];
        std::cout << "city: '" << line.city << "', id: " << line.id << ", name: '" << line.name << "'\n";
    }
}

生成的输入如

do grep -v "'" /etc/dictionaries-common/words | sort -R | xargs -d\\n -n 3 | while read a b c; do echo "$a $b;$RANDOM;$c"; done

输出就是例如。

Parsed 31609499 lines
Printing 10 random items:
city: 'opted', id: 14614, name: 'baronets theosophy'
city: 'denominated', id: 24260, name: 'insignia ophthalmic'
city: 'mademoiselles', id: 10791, name: 'smelter orienting'
city: 'ducked', id: 32155, name: 'encircled flippantly'
city: 'garotte', id: 3080, name: 'keeling South'
city: 'emirs', id: 14511, name: 'Aztecs vindicators'
city: 'characteristically', id: 5473, name: 'constancy Troy'
city: 'savvy', id: 3921, name: 'deafer terrifically'
city: 'misfitted', id: 14617, name: 'Eliot chambray'
city: 'faceless', id: 24481, name: 'shade forwent'