写磁盘必须要以512字节为单位么?
看到网上挺多说只能以512为最小单位写的。是伤磁盘还是影响写的速度。
我现在需要快速写大小不确定的文件内容,
需要等满512才能往磁盘里写?
刚才特意测了一下,感觉是每次写越多越快!
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#define TOTAL (512 * 30 * 70)
int
_write_test ( int perSize )
{
char *buffer;
DWORD dwWritten;
HANDLE hFile;
int i, times;
struct timeval start, end;
LPCTSTR fileName;
TCHAR* names[] = {TEXT("300.file"), TEXT("512.file"), TEXT("700.file")};
times = TOTAL / perSize - 1;
switch (perSize) {
case 300:
fileName = names[0];
break;
case 512:
fileName = names[1];
break;
case 700:
fileName = names[2];
break;
default:
return 0;
}
buffer = (char *) malloc(perSize);
memset(buffer, 'a', perSize);
hFile = CreateFile(
fileName,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
free(buffer);
return 0;
}
gettimeofday(&start, NULL);
printf("%lu---%lu\n", start.tv_sec, start.tv_usec);
for (i = 0; i <= times; i++) {
WriteFile(
hFile,
buffer,
perSize,
&dwWritten,
NULL
);
}
gettimeofday(&end , NULL);
printf("%lu---%lu\n", end.tv_sec, end.tv_usec);
free(buffer);
CloseHandle(hFile);
return (end.tv_sec - start.tv_sec) * 1e6 + (end.tv_usec - start.tv_usec);
}/* ----- end of function _write_test ----- */
int
main ( int argc, char *argv[] )
{
ULONG us300, us512, us700;
us512 = _write_test(512);
us700 = _write_test(700);
us300 = _write_test(300);
printf(
"us300:\t%lu\nus512:\t%lu\nus700\t%lu\n",
us300,
us512,
us700
);
return EXIT_SUCCESS;
}/* ---------- end of function main ---------- */

