小编mic*_*mic的帖子

非常快的文本文件处理(C++)

我写了一个处理GPU上数据的应用程序.代码运行良好,但我有一个问题,输入文件的读取部分(~3GB,文本)是我的应用程序的瓶颈.(从HDD读取速度很快,但逐行处理很慢).

我用getline()读取一行,将第1行复制到向量,将第2行复制到向量,并跳过第3和第4行.依此类推其余11条mio行.

我尝试了几种方法来尽可能地获取文件:

我发现最快的方法是使用boost :: iostreams :: stream

其他人是:

  • 将文件作为gzip读取,以最小化IO,但比直接读取它要慢.
  • 通过read(filepointer,chararray,length)将文件复制到ram并用循环处理它以区分行(也比boost慢)

任何建议如何让它运行得更快?

void readfastq(char *filename, int SRlength, uint32_t blocksize){
    _filelength = 0; //total datasets (each 4 lines)
    _SRlength = SRlength; //length of the 2. line
    _blocksize = blocksize;

    boost::iostreams::stream<boost::iostreams::file_source>ins(filename);
    in = ins;

    readNextBlock();
}


void readNextBlock() {
    timeval start, end;
    gettimeofday(&start, 0);

    string name;
    string seqtemp;
    string garbage;
    string phredtemp;

    _seqs.empty();
    _phred.empty();
    _names.empty();
    _filelength = 0;

            //read only a part of the file i.e the first 4mio lines
    while (std::getline(in, …
Run Code Online (Sandbox Code Playgroud)

c++ text file getline

10
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1

file ×1

getline ×1

text ×1