简体   繁体   English

C ++ ios :: out文件流标志:为什么它会影响性能?

[英]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. 我的程序正在将uint16_t的大数字(一次250,000)写入文件。 For some reason, setting the ios::out flag (unnecessary, since VS2010 sets it automatically) results in a performance decrease of roughly 10x. 出于某种原因,设置ios :: out标志(不必要,因为VS2010会自动设置它)导致性能下降大约10倍。 (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); 

之前

After: 后:

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

后

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. flags应该用于设置流的格式标志,例如输出是左对齐还是右对齐,或者布尔值应该打印为数字或字符串,因此您不能使用它来设置流的打开模式并实际调用fileoutput.flags(std::ios::out)是一个带有无效参数的调用。 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. 并且错误的可能原因在于你的std::ios::out实现值等于std::ios::unitbuf ,这会导致每个插入的缓冲区刷新,这肯定会导致巨大的性能损失。

flags functions doesn't set one flag. flags函数不设置一个标志。 It changes all the flags at once. 它会立即更改所有标志。 To modify one flag you should use setf/unsetf. 要修改一个标志,您应该使用setf / unsetf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM