Search code examples
c++iosfilestreamout

C++ ios::out file stream flag: Why does it affect performance?


My program is writing large numbers (250,000 at a time) of uint16_t's to a file. For some reason, setting the ios::out flag (unnecessary, since VS2010 sets it automatically) results in a performance decrease of roughly 10x. (see before/after). Any idea what it is about setting that flag that could cause such a huge performance difference?

Before:

fileoutput.flags(ios::out); 

Before

After:

//fileoutput.flags(ios::out);  

After


Solution

  • flags should be used to set format flags of the stream, for example whether output is left or right aligned or boolean values should printed as number or string, so you can't use it to set open mode of the stream and actually calling fileoutput.flags(std::ios::out) is a call with an invalid argument. and possible reason of the error is in your implementation value of std::ios::out is equal to std::ios::unitbuf that cause flush of buffer for each single insertion that certainly cause a huge performance penalty.