I am trying to implement fast processing of large files using the Visual Studio 2019. Data should be read, processed and then written to the end of the same file. After making some tests, I found that a file buffer of 1MB seems to be a best option on my hardware.
Here, I'm trying to set it to 1MB:
#include <fstream>
#include <array>
#include <memory>
using namespace std;
int main()
{
const streamsize BUFFER_SIZE = 1 * 1024 * 1024;
unique_ptr<::array<char, BUFFER_SIZE>> buffer = make_unique<::array<char, BUFFER_SIZE>>();
const streamsize FILE_BUFFER_SIZE = 1 * 1024 * 1024;
unique_ptr<::array<char, FILE_BUFFER_SIZE>> file_buffer = make_unique<array<char, FILE_BUFFER_SIZE>>();
ios::sync_with_stdio(false);
fstream stream;
stream.rdbuf()->pubsetbuf(file_buffer->data(), file_buffer->size());
stream.open(R"(C:\test\test_file.bin)", ios::in | ios::out | ios::binary);
while (stream.good())
{
stream.read(buffer->data(), buffer->size());
// Some data processing and writes here
}
}
While monitoring the program using the Sysinternals' ProcessMonitor, I can see that the WriteFile function is called with 1MB buffer indeed, but the ReadFile function is called 256 times for one loop iteration with only a 4K buffer. This leads to a much worse performance.
I've googled this problem and found no similar cases. I would appreciate any help on this.