저는 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; '물론 느립니다. 메모리를 재 할당하고, 내용을 복사하고, 새 행을 추가하는 데는 시간이 걸립니다. 오스카 (Oscar)는 그런 접근 방식을 다시 생각해 볼 수도 있습니다. –
'out << id << ":"<< len << endl;'endl'은 줄 바꿈 만하는 것이 아닙니다. 또한 버퍼를 플러시합니다. 필요하지 않은 경우에는 사용하지 마십시오. 그냥'\ n'을 사용하십시오. – Matt