Re: fstream "write" faster than Windows "WriteFile"
<stefan.kniep@gmx.de> wrote in message
news:1188315406.474306.274660@r34g2000hsd.googlegroups.com...
Thank your for your answers.
Bottom line is this: there's no way that ofstream is faster than
WriteFile.
Under the covers - under about 4 layers of abstraction - writing with
ofstream in fact calls WriteFile to do the writing.
Agreed. Maybe I should have been more precise: Why is my call to
WriteFile slower than fstream write?
To get the fastest write performance you should:
- Use CreateFile with FILE_FLAG_NO_BUFFERING and FILE_FLAG_OVERLAPPED.
- Do asynchronous writes using WriteFile.
- Write only in aligned multiples of the disk sector size.
- Write as much data from memory in a single call to WriteFile as you
can.
In my sample code, the file is written with one WriteFile call, and no
other operations are pending on the file. Thus the performance
shouldn't be affected by FILE_FLAG_OVERLAPPED. Is that right?
Unfortenately, I cannot use FILE_FLAG_NO_BUFFERING in the project I am
working on, because I cannot meet the requirements for this flag. I
Of course you can meet them. You just need to account for alignment when
you create your buffers, that might mean using VirtualAlloc to make your
array instead of operator new.
guessed that fstream write also doesn't use them, because the address
of the buffer passed to fstream write isn't required to be sector
aligned. *puzzled*
Nobody said fwrite couldn't realign your data internally. It almost
certainly does so. The speed increase from aligned DMA is enough to make up
for the cost of the copy. If you align your data to begin with you can get
rid of the copy as well.