On the English version of stackoverflow, I came across this question:

std :: fstream buffering vs manual buffering

Do not really understand what is happening in the write method to the buffer directly? (there is this moment omitted). I understand that you are recording directly via streambuf::sputn (i.e. stream.rdbuf() ->sputn(buffer, length) )? And why is std::ostream::write called at the end, as in the first method? After all, data is already directly written to the buffer.

    1 answer 1

    In the second variant, there is simply already a ready filled buffer, which is written entirely to the file. Most likely, it was filled with random numbers, or manually in a cycle, but this is not known. In the worst case, it is not filled at all, and garbage is written to the file.

    In the first variant, the buffer is initially empty, it is transferred under the control of the flow, and many small objects are written there via the flow interface. More precisely, they are written to the file via the buffer.

    Considering that neither the method of filling the buffer in the second variant, nor the size of the object x in the first variant are indicated, I would not trust the results of the tests.