私はC++で非常に新しいです!それで、あなたがそれを考慮し、可能な限り簡単に答えることができたら、本当に感謝しています。私は> 40000シーケンス(500Mb近く)でfastaファイルを解析し、IDとシーケンス長を新しいファイルに書き込む必要があります。私はそれがC++で非常に遅くなっていることを発見しました。この目的のために、Pythonははるかに高速に動作します。しかし、私はC++でどのようにできるのかを知る必要があります。私はこのプロセスをC++に固定する方法があるのだろうか?大きなファイルを読み書きする
これは私のコードです:事前に
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <stdio.h>
using namespace std;
int main() {
time_t start, end;
time(&start);
clock_t begin = clock();
ifstream file;
string line;
string id;
string content;
int len = 0;
int i = 0;
ofstream out;
file.open("contigs.fasta", ios::in);
out.open("output.txt", ios::out);
while (getline(file, line)) {
if (line[0] == '>') {
i++;
if (i != 1) {
//cout << id << "\n" << len << "\n" << content << endl;
//out.write(line.c_str(), line.size());
out << id << " : " << len << endl;
}
id = line;
len = 0;
content = "";
}
else
{
len += line.length();
content += line;
}
}
//cout << id << "\n" << len << "\n" << content << endl;
//out << id << " : " << len << endl;
cout << "Total number of sequences :" << i << "\n";
out.close();
time (&end);
double dif = difftime (end,start);
printf ("Elasped time is %.2lf seconds.", dif);
return 0;
}
ありがとう!
ファイル全体を一度に読むのではなく、小さな塊で読む。 http://stackoverflow.com/questions/20911584/how-to-read-a-file-in-multiple-chunks-until-eof-c – Oscar
'content + = line;'もちろん、遅いです。メモリの再割り当て、コンテンツのコピー、新しい行の追加、これには時間がかかります。オスカー氏のように、そのアプローチを再考したいかもしれません。 –
'out << id <<": "<< len << endl;' endl'は単に新しい行を与えるだけではありません。また、バッファをフラッシュします。必要がない場合は使用しないでください。 '\ n'を使うだけです。 – Matt