C++如何快速的写大文件
要讲内存Buffer快速写入硬盘,可以分多个文件或者一个文件,怎样操作最快?
我选择了分多个文件来写,做了一个简单的测试,发现一个奇怪的问题,写前三个文件速度很快几乎为0,到第四个文件速度就很慢了,这个怎么解释呢?
1G文件基本要在10秒以上。
//Begin
ULONG ll_size=1024*1024*12*10;
BYTE * l_pBuffer=new BYTE[ll_size];
memset(l_pBuffer,0,ll_size);
//write
string ls_dir="H:\\temp\";
string ls_ext=".dat";
ULONG ll_start=clock();
ULONG ll_begin,ll_end;
ofstream file_buffer;
for (ULONG ll_n=0;ll_n<3;ll_n++)
{
ll_begin=clock();
string ls_path=ls_dir+num2str(ll_n)+ls_ext;
file_buffer.open(ls_path.c_str(),ios::binary);
for (ULONG ll_i=0;ll_i<1;ll_i++)
{
file_buffer.write((char *)l_pBuffer,ll_size);
}
file_buffer.close();
ll_end=clock();
cout<<(ll_end -ll_begin)/CLOCKS_PER_SEC<<std::endl;
}
system("pause");
for (ULONG ll_n=3;ll_n<6;ll_n++)
{
ll_begin=clock();
string ls_path=ls_dir+num2str(ll_n)+ls_ext;
file_buffer.open(ls_path.c_str(),ios::binary);
for (ULONG ll_i=0;ll_i<1;ll_i++)
{
file_buffer.write((char *)l_pBuffer,ll_size);
}
file_buffer.close();
ll_end=clock();
cout<<(ll_end -ll_begin)/CLOCKS_PER_SEC<<std::endl;
}
ULONG ll_stop=clock();
cout<<"Total:";
cout<<(ll_stop -ll_start)/CLOCKS_PER_SEC<<std::endl;
system("pause");