I have been trying to understand this problem for about 6 months.
A few years ago in the old C++ one would use <fstream.h> which had a nice little function called setbuf().
ofstream myFile("source", ios::binary);
myFile.setbuf(buffer, size);
myFile.write(massivedata, int)...
which optimized I/O via a user-defined buffer. Trust me, when moving large amounts of data to files, it increased the I/O speed by a HUGE factor, often 10x as fast as a simple binary write. All my programs used it.
But C++ stream buffers are handled differently now with the new C++ classes defined in the file <fstream>. Here, an ofstream's setbuf() function is protected not public. I am told the way to set the buffer is to use the rdbuf() member and it via a public access function:
ofstream myFile("source", ios::binary);
myFile.rdbuf()->pubsetbuf(buffer, size);
myFile.write(massivedata, int)...
I'll tell you this, my friends: the new <fstream> class appears to be far slower than <fstream.h> in every possible way... in fact, using <fstream.h> without the optimization from setbuf() is twice as fast as the <fstream> class. Accessing rdbuf() through its public interface does nothing to the speed whatsoever.
I understand that the efficacy of buffering depends on experimenting with the size of the buffer; I have done this for several months and cannot replicate the speed I consistently get with <fstream.h>.
It is good programming discipline to use the most current C++ classes, so I would like to update to <fstream> rather than <fstream.h>.
Does anyone out there have any experience on this topic? Am I using fstream buffers wrongly?
A few years ago in the old C++ one would use <fstream.h> which had a nice little function called setbuf().
ofstream myFile("source", ios::binary);
myFile.setbuf(buffer, size);
myFile.write(massivedata, int)...
which optimized I/O via a user-defined buffer. Trust me, when moving large amounts of data to files, it increased the I/O speed by a HUGE factor, often 10x as fast as a simple binary write. All my programs used it.
But C++ stream buffers are handled differently now with the new C++ classes defined in the file <fstream>. Here, an ofstream's setbuf() function is protected not public. I am told the way to set the buffer is to use the rdbuf() member and it via a public access function:
ofstream myFile("source", ios::binary);
myFile.rdbuf()->pubsetbuf(buffer, size);
myFile.write(massivedata, int)...
I'll tell you this, my friends: the new <fstream> class appears to be far slower than <fstream.h> in every possible way... in fact, using <fstream.h> without the optimization from setbuf() is twice as fast as the <fstream> class. Accessing rdbuf() through its public interface does nothing to the speed whatsoever.
I understand that the efficacy of buffering depends on experimenting with the size of the buffer; I have done this for several months and cannot replicate the speed I consistently get with <fstream.h>.
It is good programming discipline to use the most current C++ classes, so I would like to update to <fstream> rather than <fstream.h>.
Does anyone out there have any experience on this topic? Am I using fstream buffers wrongly?