假设我有一个类似如下的文件:
*SP "<something>"
*VER "<something>"
*NAME_MAP
*1 abc
*2 def
...
...
*D_NET *1 <some_value>
*CONN
<whatever>
<whatever>
*CAP
*1:1 *2:2 <whatever_value>
*1:3 *2:4 <whatever_value>
*RES
<whatever>
<whatever>在开始描述我的问题之前,让我先描述一下文件。文件以一些标题注释开始。NAME_MAP部分包含有关为其提供的名称和id的映射信息。当我想要指定相应的名称时,该id将在以后的任何地方使用。
D_NET部分有3个子部分,CONN,CAP,RES。
我需要从这个文件中收集一些数据。我需要的数据与D_NET相关。我需要
*D_NET *1 <some_value>从此行映射*1,在本例中为abc。
我需要的第二件事是来自D_NET部分的CAP部分。无论CAP部分有什么,我都需要它。
最后,我的数据看起来像一个散列:
*1 -> *1,*2 (在本例中,只是为了让您理解) abc -> abc,def (这就是我想要的)
希望到目前为止我已经说清楚了。
由于文件很大,以Gb为单位,我发现读取文件的最好方法是将其映射到内存中。使用mmap可以做到这一点。就像这样:
char* data = (char*)mmap(0, file.st_size, PROT_READ, MAP_PRIVATE, fileno(file), 0);因此,mmap所指向的数据只是一个字符流。现在,我需要从其中获取上述数据。
为了解决这个问题,我想我可以使用一些tokenizer(boost/tokenizer?)这里首先拆分新行字符,然后解析这些行以获得所需的数据。谁会同意我的观点呢?如果你不同意这一点,你还有什么建议吗?请提个建议。
你有什么建议吗?我对任何快速算法都持开放态度。
发布于 2018-12-05 22:01:26
我对您希望通过使用mmap获得的性能感到好奇,所以我将两个测试放在一起,从我的媒体库中读取文件(将它们视为文本文件)。一个使用getline方法,另一个使用mmap。输入是:
files: 2012
lines: 135371784
bytes: 33501265769 (31 GiB)首先,在两个测试中使用一个辅助类来读取文件列表:
filelist.hpp
#pragma once
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
class Filelist {
std::vector<std::string> m_strings;
public:
Filelist(const std::string& file) :
m_strings()
{
std::ifstream is(file);
for(std::string line; std::getline(is, line);) {
m_strings.emplace_back(std::move(line));
}
/*
std::copy(
std::istream_iterator<std::string>(is),
std::istream_iterator<std::string>(),
std::back_inserter(m_strings)
);
*/
}
operator std::vector<std::string> () { return m_strings; }
};getline.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
#include "filelist.hpp"
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
if(args.size()==0) {
Filelist tmp("all_files");
args = tmp;
}
unsigned long long total_lines=0;
unsigned long long total_bytes=0;
for(const auto& file : args) {
std::ifstream is(file);
if(is) {
unsigned long long lco=0;
unsigned long long bco=0;
bool is_good=false;
for(std::string line; std::getline(is, line); lco+=is_good) {
is_good = is.good();
bco += line.size() + is_good;
// parse here
}
std::cout << std::setw(15) << lco << " " << file << "\n";
total_lines += lco;
total_bytes += bco;
}
}
std::cout << "files processed: " << args.size() << "\n";
std::cout << "lines processed: " << total_lines << "\n";
std::cout << "bytes processed: " << total_bytes << "\n";
}getline结果:
files processed: 2012
lines processed: 135371784
bytes processed: 33501265769
real 2m6.096s
user 0m23.586s
sys 0m20.560smmap.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include "filelist.hpp"
#include <sys/mman.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
class File {
int m_fileno;
public:
File(const std::string& filename) :
m_fileno(open(filename.c_str(), O_RDONLY, O_CLOEXEC))
{
if(m_fileno==-1)
throw std::runtime_error("could not open file");
}
File(const File&) = delete;
File(File&& other) :
m_fileno(other.m_fileno)
{
other.m_fileno = -1;
}
File& operator=(const File&) = delete;
File& operator=(File&& other) {
if(m_fileno!=-1) close(m_fileno);
m_fileno = other.m_fileno;
other.m_fileno = -1;
return *this;
}
~File() {
if(m_fileno!=-1) close(m_fileno);
}
operator int () { return m_fileno; }
};
class Mmap {
File m_file;
struct stat m_statbuf;
char* m_data;
const char* m_end;
struct stat pstat(int fd) {
struct stat rv;
if(fstat(fd, &rv)==-1)
throw std::runtime_error("stat failed");
return rv;
}
public:
Mmap(const Mmap&) = delete;
Mmap(Mmap&& other) :
m_file(std::move(other.m_file)),
m_statbuf(std::move(other.m_statbuf)),
m_data(other.m_data),
m_end(other.m_end)
{
other.m_data = nullptr;
}
Mmap& operator=(const Mmap&) = delete;
Mmap& operator=(Mmap&& other) {
m_file = std::move(other.m_file);
m_statbuf = std::move(other.m_statbuf);
m_data = other.m_data;
m_end = other.m_end;
other.m_data = nullptr;
return *this;
}
Mmap(const std::string& filename) :
m_file(filename),
m_statbuf(pstat(m_file)),
m_data(reinterpret_cast<char*>(mmap(0, m_statbuf.st_size, PROT_READ, MAP_PRIVATE, m_file, 0))),
m_end(nullptr)
{
if(m_data==MAP_FAILED)
throw std::runtime_error("mmap failed");
m_end = m_data+size();
}
~Mmap() {
if(m_data!=nullptr)
munmap(m_data, m_statbuf.st_size);
}
inline size_t size() const { return m_statbuf.st_size; }
operator const char* () { return m_data; }
inline const char* cbegin() const { return m_data; }
inline const char* cend() const { return m_end; }
inline const char* begin() const { return cbegin(); }
inline const char* end() const { return cend(); }
};
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
if(args.size()==0) {
Filelist tmp("all_files");
args = tmp;
}
unsigned long long total_lines=0;
unsigned long long total_bytes=0;
for(const auto& file : args) {
try {
unsigned long long lco=0;
unsigned long long bco=0;
Mmap im(file);
for(auto ch : im) {
if(ch=='\n') ++lco;
++bco;
}
std::cout << std::setw(15) << lco << " " << file << "\n";
total_lines += lco;
total_bytes += bco;
} catch(const std::exception& ex) {
std::clog << "Exception: " << file << " " << ex.what() << "\n";
}
}
std::cout << "files processed: " << args.size() << "\n";
std::cout << "lines processed: " << total_lines << "\n";
std::cout << "bytes processed: " << total_bytes << "\n";
}mmap结果:
files processed: 2012
lines processed: 135371784
bytes processed: 33501265769
real 2m8.289s
user 0m51.862s
sys 0m12.335s我在每次测试后立即运行测试,如下所示:
% ./mmap
% time ./getline
% time ./mmap..。他们得到了非常相似的结果。如果我处于您的位置,我会首先选择简单的getline解决方案,然后尝试将逻辑与您已有的映射放在适当的位置。如果以后觉得这很慢,如果你能找到一些方法让它比我更有效,那就去mmap。
免责声明:我没有太多使用mmap的经验,所以也许我错误地使用了它来获得它解析文本文件所能提供的性能。
更新:我将所有文件连接到一个31 GiB文件中,并再次运行测试。结果有点令人惊讶,我觉得我错过了一些东西。
getline结果:
files processed: 1
lines processed: 135371784
bytes processed: 33501265769
real 2m1.104s
user 0m22.274s
sys 0m19.860smmap结果:
files processed: 1
lines processed: 135371784
bytes processed: 33501265769
real 2m22.500s
user 0m50.183s
sys 0m13.124shttps://stackoverflow.com/questions/53608876
复制相似问题