바이너리 파일을 쓰는 제일 빠른 방법
조회수 4294회
SSD에 80GB 정도 되는 데이터를 써야 합니다. 제 코드는 윈도우7, 비주얼 2010에서 돌렸을 때 20MB/s 정도로 쓰던데 제가 알기론 SSD간에 파일 전송은 150~200MB까지 가능한 걸로 알고 있어서 제 코드는 너무 비효율적인 것 같아요.
이론상으로는 7배쯤 더 빨리쓸 수 있는 방법이 있을 것 같은데 더 좋은 소스코드를 알려주세요!
소스코드
#include <fstream>
using namespace std;
const unsigned long long size = 64ULL*1024ULL*1024ULL;
unsigned long long a[size];
int main()
{
fstream myfile;
myfile = fstream("file.binary", ios::out | ios::binary);
//Here would be some error handling
for(int i = 0; i < 32; ++i){
//Some calculations to fill a[]
myfile.write((char*)&a,size*sizeof(unsigned long long));
}
myfile.close();
}
1 답변
-
제 컴퓨터에서 실험했을 때(SSD) 8GB 다 쓰는데 36초 정도(약 220MB/s) 걸렸습니다. SSD에서 쓸 수 있는 최대한으로 쓴 것 같네요.
#include <stdio.h> const unsigned long long size = 8ULL*1024ULL*1024ULL; unsigned long long a[size]; int main() { FILE* pFile; pFile = fopen("file.binary", "wb"); for (unsigned long long j = 0; j < 1024; ++j){ //Some calculations to fill a[] fwrite(a, 1, size*sizeof(unsigned long long), pFile); } fclose(pFile); return 0; }
댓글 입력