今天测试了一下读写文件时缓冲区多大合适的问题,缓冲区的大小并非我之前想的那样越大越好,测试后发现竟然是64KB时最快啊。下面是测试用的代码大家看一下有问题请指正:
HANDLE h = CreateFile(m_file_name,FILE_ALL_ACCESS,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (h==INVALID_HANDLE_VALUE)
{
return GetLastError();
}
static char read_buff[READ_SIZE];
DWORD tick_count = GetTickCount();
while(1)
{
DWORD len = 0;
BOOL ret = ReadFile(h,read_buff,READ_SIZE,&len,NULL);
if (!ret)
{
return GetLastError();
}
else if (len == 0)
{
break; //文件结束了
}
}
DWORD time = GetTickCount() - tick_count;
sprintf(read_buff,"time = %lu./n",time);
OutputDebugString(read_buff);
CloseHandle(h);