First of all, the iostream library is already buffered under-the-hood. So, there isn't much reason for you to add another buffer on top of that (although it might help). The reason for your slow performance with this line:
flowC << prevsid << ", " << threadid << endl;
is in part due to ascii output (or formatted output), but mainly due to the presence of endl
at the end of the output. The endl
thing has two effects: adds a new-line to the output; and flushes the stream. The latter effect means that everything that is currently pending on the stream's internal buffer will be flushed, i.e., physically written to the file. So, the main problem here is that you are flushing the buffer every time in that loop. Simply replacing the endl
with a new-line character will make a big difference because the stream will essentially do exactly the kind of buffering you were trying to do yourself (write stuff to a temporary buffer until it is too full and then flush it to the file). So, try this instead:
flowC << prevsid << ", " << threadid << "\n";
If you actually want to do some buffering on your own, on top of the buffering that is already done by the file-stream object, then I would recommend that you use a std::stringstream
instead. As follows:
std::stringstream ss_buffer;
.......
// in the loop:
ss_buffer << prevsid << ", " << threadid << …