Slow file IO on larger sized file.

%3CLINGO-SUB%20id%3D%22lingo-sub-653248%22%20slang%3D%22en-US%22%3ESlow%20file%20IO%20on%20larger%20sized%20file.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-653248%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20the%20following%20codes%20to%20open%20a%20host%20file%2C%20write%20one%20line%20at%20the%20beginning%20of%20the%20file%20then%20close%20this%20file.%20It%20performed%20much%20slower%20on%20a%20large%20file%20(175%2C000%20bytes)%20than%20on%20a%20small%20file%20(35%20bytes).%20The%20time%20spent%20mostly%20on%20the%20CloseHandle()%20system%20call.%20Does%20anyone%20have%20the%20same%20problem%3F%20Is%20it%20a%20Microsoft%20Windows's%20bug%3F%20%26nbsp%3BAny%20response%20is%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%2F%2F%20filetest.c%20%3A%20Testing%20performance%20of%20file%20IO%20in%20Widnows.%3C%2FP%3E%3CP%3E%2F%2F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%23include%3C%2FSPAN%3E%20%3CWINDOWS.H%3E%3C%2FWINDOWS.H%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%23include%3C%2FSPAN%3E%20%3CSTDIO.H%3E%3C%2FSTDIO.H%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%23include%3C%2FSPAN%3E%20%3CSTRING.H%3E%3C%2FSTRING.H%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Echar%3C%2FSPAN%3E%3CSPAN%3E%20*data%20%3D%20%3C%2FSPAN%3E%22The%20line%20to%20be%20put%20in%20messages.log%5Cn%22%3CSPAN%3E%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Eint%3C%2FSPAN%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%3C%2FSPAN%3Elogtofile(%3CSPAN%3Echar%3C%2FSPAN%3E%3CSPAN%3E%20*%3C%2FSPAN%3Efilename%3CSPAN%3E%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Echar%3C%2FSPAN%3E%20*%3CSPAN%3Ebuf%3C%2FSPAN%3E%2C%26nbsp%3B%3CSPAN%3Eint%3C%2FSPAN%3E%20%3CSPAN%3Esiz%3C%2FSPAN%3E)%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eint%3C%2FSPAN%3E%3CSPAN%3E%20j%3B%20%3C%2FSPAN%3E%2F*%20timeout%20*%2F%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eint%3C%2FSPAN%3E%20nw%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3EHANDLE%3C%2FSPAN%3E%20han%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bj%20%3D%2010%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Edo%3C%2FSPAN%3E%20%7B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3E%3CSPAN%3EDWORD%3C%2FSPAN%3E%20lasterror%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3Ehan%20%3D%20CreateFileA(%3CSPAN%3Efilename%3C%2FSPAN%3E%2C%3CSPAN%3EGENERIC_WRITE%3C%2FSPAN%3E%2C%3C%2FP%3E%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3E%3C%2FSPAN%3EFILE_SHARE_READ%3CSPAN%3E%2C%3C%2FSPAN%3ENULL%3CSPAN%3E%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3C%2FSPAN%3EOPEN_ALWAYS%3CSPAN%3E%2C%3C%2FSPAN%3EFILE_ATTRIBUTE_NORMAL%3CSPAN%3E%2C%3C%2FSPAN%3ENULL%3CSPAN%3E)%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20lasterror%20%3D%20GetLastError()%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%3Eif%3C%2FSPAN%3E%3CSPAN%3E%20(han%20%3D%3D%20%3C%2FSPAN%3E%3CSPAN%3EINVALID_HANDLE_VALUE%3C%2FSPAN%3E%3CSPAN%3E)%20%7B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eif%3C%2FSPAN%3E%20(lasterror%20%3D%3D%20%3CSPAN%3EERROR_SHARING_VIOLATION%3C%2FSPAN%3E)%20Sleep(5)%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eelse%3C%2FSPAN%3E%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%3Eprintf(%3C%2FSPAN%3E%22%5Cn%20CreatFile%20Failed%20error%3D%25d%22%3CSPAN%3E%2Clasterror)%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%3Ereturn%3C%2FSPAN%3E%200%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%7D%20%3CSPAN%3Ewhile%3C%2FSPAN%3E%20((han%20%3D%3D%20%3CSPAN%3EINVALID_HANDLE_VALUE%3C%2FSPAN%3E)%20%26amp%3B%26amp%3B%20--j)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eif%3C%2FSPAN%3E%3CSPAN%3E%20(han%20!%3D%20%3C%2FSPAN%3EINVALID_HANDLE_VALUE%3CSPAN%3E)%20%7B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3Ej%20%3D%20WriteFile(han%2C%3CSPAN%3Ebuf%3C%2FSPAN%3E%2C%3CSPAN%3Esiz%3C%2FSPAN%3E%2C%26amp%3Bnw%2C%3CSPAN%3ENULL%3C%2FSPAN%3E)%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eif%3C%2FSPAN%3E%20(!j)%20%7B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3Eprintf(%3CSPAN%3E%22%5Cnlogtofile%20error%3D%25d%22%3C%2FSPAN%3E%2CGetLastError())%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bj%20%3D%20CloseHandle(han)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Breturn%3CSPAN%3E%200%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Eint%3C%2FSPAN%3E%20main(%3CSPAN%3Eint%3C%2FSPAN%3E%20%3CSPAN%3Eargc%3C%2FSPAN%3E%2C%20%3CSPAN%3Echar%3C%2FSPAN%3E*%20%3CSPAN%3Eargv%3C%2FSPAN%3E%5B%5D)%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Echar%3C%2FSPAN%3E%20*nam%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eint%3C%2FSPAN%3E%20iter%2C%20i%2C%20len%2C%20msec%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Etime_t%3C%2FSPAN%3E%20sec%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eint%3C%2FSPAN%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3Emsec%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Estruct%3C%2FSPAN%3E%20%3CSPAN%3Etimeb%3C%2FSPAN%3E%20starttimeb%2C%20endtimeb%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eif%3C%2FSPAN%3E%20(%3CSPAN%3Eargc%3C%2FSPAN%3E%20%3D%3D%201)%20%7B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eprintf(%3C%2FSPAN%3E%22usage%3A%20filetest%20%3CFNAME%3E%20%5Biteration%5D%20%22%3CSPAN%3E)%3B%3C%2FSPAN%3E%3C%2FFNAME%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3Ereturn%200%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bnam%20%3D%20%3CSPAN%3Eargv%3C%2FSPAN%3E%5B1%5D%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Blen%20%3D%20strlen(data)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eif%3C%2FSPAN%3E%20(%3CSPAN%3Eargc%3C%2FSPAN%3E%20%26gt%3B%202)%20iter%20%3D%20atol(%3CSPAN%3Eargv%3C%2FSPAN%3E%5B2%5D)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eelse%3C%2FSPAN%3E%20iter%20%3D%205000%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bftime(%26amp%3Bstarttimeb)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Efor%3C%2FSPAN%3E%20(i%20%3D%200%3B%20i%20%26lt%3B%20iter%3B%20i%2B%2B)%20%7B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3Elogtofile(nam%2Cdata%2Clen)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bftime(%26amp%3Bendtimeb)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bsec%20%3D%20endtimeb.time%20-%20starttimeb.time%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eif%3C%2FSPAN%3E%20(endtimeb.millitm%20%26lt%3B%20starttimeb.millitm)%20%7B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3Esec--%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3Emsec%20%3D%20endtimeb.millitm%20%2B%201000%20-%20starttimeb.millitm%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%7D%20%3CSPAN%3Eelse%3C%2FSPAN%3E%20%7B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22Apple-converted-space%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3Emsec%20%3D%20endtimeb.millitm%20-%20starttimeb.millitm%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3CSPAN%3Eprintf(%3C%2FSPAN%3E%22%5CnSecond%3A%20%25lld%2C%20millisecond%3A%20%25d%5Cn%22%3CSPAN%3E%2Csec%2Cmsec)%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Breturn%3CSPAN%3E%200%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-653248%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Esimonl%40intersystems.com%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional Visitor

I have the following codes to open a host file, write one line at the beginning of the file then close this file. It performed much slower on a large file (175,000 bytes) than on a small file (35 bytes). The time spent mostly on the CloseHandle() system call. Does anyone have the same problem? Is it a Microsoft Windows's bug?  Any response is appreciated.

 

// filetest.c : Testing performance of file IO in Widnows.

//

 

#include <Windows.h>

#include <stdio.h>

#include <string.h>

 

char *data = "The line to be put in messages.log\n";

 

int  logtofile(char *filenamechar *bufint siz)

{

   int j; /* timeout */

   int nw;

   HANDLE han;

 

   j = 10;

   do {

      DWORD lasterror;

      han = CreateFileA(filename,GENERIC_WRITE,

                                    FILE_SHARE_READ,NULL,

                                   OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

      lasterror = GetLastError();

      if (han == INVALID_HANDLE_VALUE) {

         if (lasterror == ERROR_SHARING_VIOLATION) Sleep(5);

         else {

            printf("\n CreatFile Failed error=%d",lasterror);

            return 0;

         }

      }

   } while ((han == INVALID_HANDLE_VALUE) && --j);

   if (han != INVALID_HANDLE_VALUE) {

      j = WriteFile(han,buf,siz,&nw,NULL);

      if (!j) {

         printf("\nlogtofile error=%d",GetLastError());

      }

   }

   j = CloseHandle(han);

   return 0;

}

 

int main(int argc, char* argv[])

{

   char *nam;

   int iter, i, len, msec;

   time_t sec;

   int    msec;

   struct timeb starttimeb, endtimeb;

 

   if (argc == 1) {

     printf("usage: filetest <fname> [iteration] ");

     return 0;

   }

   nam = argv[1];

   len = strlen(data);

 

   if (argc > 2) iter = atol(argv[2]);

   else iter = 5000;

   ftime(&starttimeb);

   for (i = 0; i < iter; i++) {

     logtofile(nam,data,len);

   }

   ftime(&endtimeb);

   sec = endtimeb.time - starttimeb.time;

   if (endtimeb.millitm < starttimeb.millitm) {

     sec--;

     msec = endtimeb.millitm + 1000 - starttimeb.millitm;

   } else {

     msec = endtimeb.millitm - starttimeb.millitm;

   }

   printf("\nSecond: %lld, millisecond: %d\n",sec,msec);

   return 0;

}

1 Reply
Highlighted
Hi,
please try Stackoverflow if you still have the problem
https://stackoverflow.com/questions