Why C++ fread is so different from Windows ReadFile?
-
lunedì 27 febbraio 2012 08:25
Greetings,
I write a piece of code to simulate system I/O perf. However the result is so different between fread (IOPS is 5000+) and Windows ReadFile (IOPS 200 ~ 300) for a 7200RPM HDD (it's 180 ~ 190 Random read IOPS by IOMeter). Could you give me a hint where's the problem?
Thanks.
Nai Yan.Code - Windows ReadFile (with cache)
DWORD WINAPI FileReadThreadEntry(LPVOID lpThreadParameter) { //File access with cache enabled input * in = (input*) lpThreadParameter; LPCTSTR path = (LPCTSTR) in->path; HANDLE fp = CreateFile( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,//|FILE_FLAG_NO_BUFFERING, NULL); unsigned int sPos = in->starting; int * result = in->r; if(fp != NULL) { unsigned long pos; bool bRead; DWORD bNum; for (int i=0; i<length/(threadCount*interval);i++) { pos = i * interval; if ((SetFilePointer(fp,(sPos+pos),NULL,FILE_BEGIN)!=0)) { if ((bRead = ReadFile(fp,&result[sPos/interval + i],512,&bNum,NULL))== true) { InterlockedIncrement(&completeIOs); } else { printf("file read err...\n"); exit(-1); } } } CloseHandle(fp); fp = NULL; } else { printf("File open err... \n"); exit(-1); } }C++ fread code
DWORD WINAPI FileReadThreadEntry(LPVOID lpThreadParameter) { input * in = (input*) lpThreadParameter; LPCTSTR path = (LPCTSTR) in->path; FILE * fp = fopen(path,"rb"); int sPos = in->starting; int * result = in->r; if(fp != NULL) { fpos_t pos; for (int i=0; i<length/(threadCount*interval);i++) { pos = i * interval; fsetpos(fp,&pos); if (fread(&result[sPos/interval + i],512,1,fp) ==1) { InterlockedIncrement(&completeIOs); } else { printf("file read err...\n"); exit(-1); } } fclose(fp); fp = NULL; } else { printf("File open err... \n"); exit(-1); } }
Tutte le risposte
-
giovedì 1 marzo 2012 19:48Proprietario
Hello,
This forum is for answering questions related to C++ AMP, PPL, ConcRT and other C++ related threading questions.
Since the ask is specifically around the performance of ReadFile, your best to get a response is to post here: http://answers.microsoft.com/en-us/windows/forum
Thanks!
Rahul V. Patil
- Proposto come risposta Zhu, Weirong venerdì 2 marzo 2012 16:46
- Contrassegnato come risposta DanielMothMicrosoft Employee, Owner domenica 10 giugno 2012 06:23

