hi all
i have a program for reading/writing files using windows API
this program works properly
but i have problem with program speed
the time of reading the file is good but it takes very more time for writing the file
here is the standard source code that my program uses a source like this for reading/writing the file
you can test it on any version of C++Builder (or any other IDE?) and see the resulted time (please only call writefile and readfile functions)

#include <DateUtils.hpp>

HANDLE OpenWrite(const char* fname) {
     HANDLE fileHandle = CreateFile( fname,
                                 GENERIC_WRITE,
                                 0,
                                 NULL,
                                 CREATE_ALWAYS,
                                 FILE_ATTRIBUTE_NORMAL,
                                 NULL );

     if (fileHandle == INVALID_HANDLE_VALUE)
       fileHandle = NULL;
     return fileHandle;
}


HANDLE OpenRead(const char* fname) {
     HANDLE fileHandle = CreateFile( fname,
                                 GENERIC_READ,
                                 0,
                                 NULL,
                                 OPEN_EXISTING,
                                 FILE_ATTRIBUTE_NORMAL,
                                 NULL );
     if (fileHandle == INVALID_HANDLE_VALUE)
       fileHandle = NULL;
     return fileHandle;
}


void writefile(){
   int n[100000];
   DWORD dwrt = 0;
   HANDLE out=NULL;

   out = OpenWrite("test");
   if (out == NULL){
    ShowMessage("Cannot open file.");
    exit(0);
   }

   for (int pos = 0; pos < 100000; pos++) {
     n[pos] = 1;
   }

   TTime t1=Time ();
    WriteFile(out,n,sizeof(n),&dwrt,NULL);
   TTime t2=Time ();
   ShowMessage("time of writing : "+ IntToStr(MilliSecondsBetween(t1,t2)));

   CloseHandle(out);
   out = NULL;
}


void readfile(){
   int n[100000];
   HANDLE in=NULL;
   DWORD dwrd = 0;

   in = OpenRead("test");
   if (in == NULL){
     ShowMessage("Cannot open file.");
     exit(0);
   }

   TTime t1=Time ();
    ReadFile(in,n,sizeof(n),&dwrt,NULL);
   TTime t2=Time ();
   ShowMessage("time of reading : "+ IntToStr(MilliSecondsBetween(t1,t2)));

   CloseHandle(in);
   in = NULL;
}
William Hemsworth commented: Code tags on first post :) +2

Recommended Answers

All 4 Replies

btw : please use
ReadFile(in,n,sizeof(n),&dwrd,NULL);
in readfile function
i couldn't see any edit button

Reading is usually faster than writing for pretty much all non-volatile mediums (ROM, eeprom, flash (e.g. pendrives), harddisk, pen on paper, etc.).

Most operating systems cache all disk access where possible - this makes writes appear much quicker (but not for large files), and makes reads heaps quicker (after the first read). On my system, sustained writes are at ~5MB/s and cached writes (for a 1.7MB file) was ~64MB/s. Reads, you will see from the following output become faster after the initial read:

Wrote 1728000 / 1728000 in 0.0271588 seconds (63.6259 MB/s)
Read 1728000 / 1728000 in 0.238114 seconds (7.25703 MB/s)
Wrote 1728000 / 1728000 in 0.0263265 seconds (65.6372 MB/s)
Read 1728000 / 1728000 in 0.00225057 seconds (767.807 MB/s)
Wrote 1728000 / 1728000 in 0.026752 seconds (64.5933 MB/s)
Read 1728000 / 1728000 in 0.00217849 seconds (793.21 MB/s)
Wrote 1728000 / 1728000 in 0.0267098 seconds (64.6953 MB/s)
Read 1728000 / 1728000 in 0.00206004 seconds (838.819 MB/s)
Wrote 1728000 / 1728000 in 0.0267693 seconds (64.5515 MB/s)
Read 1728000 / 1728000 in 0.00205724 seconds (839.958 MB/s)

My point is that writes are slower than reads, and that the OS file caching will make your timing measurements more difficult (you will have to use very large file sizes)

Sorry, I don't know the answer to speeding up ofstream writes. The WriteFile API I would assume would be the fastest. You can always try using the good old 'C' fopen, fwrite(), etc...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.