我正在尝试将映射的文件读入矩阵。该文件是这样的:
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的内容,但是我不知道如何使用它来加快我的代码的速度。
最佳答案
我有很多这样的/类似的例子写在SO上。
让我列出最相关的内容:
在所有其他情况下,请考虑猛烈执行它的Spirit Qi作业,可能使用
boost::string_ref
而不是vector<char>
(当然,除非映射文件不是“const”)。string_ref
也显示在之前链接的最后一个答案中。另一个有趣的示例(将延迟转换为未转义的字符串值)在这里How to parse mustache with Boost.Xpressive correctly?演示
这是Qi工作受到的打击:
struct Line {
boost::string_ref name, city;
long id;
};
string_ref
)来存储字符串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'