std :: fstream缓冲vs手动缓冲(为什么10倍增益与手动缓冲)? [英] std::fstream buffering vs manual buffering (why 10x gain with manual buffering)?
问题描述
我已测试过两种写入配置:
I have tested two writing configurations :
1)Fstream缓冲:
1) Fstream buffering :
// Initialization
const unsigned int length = 8192;
char buffer[length];
std::ofstream stream;
stream.rdbuf()->pubsetbuf(buffer, length);
stream.open("test.dat", std::ios::binary | std::ios::trunc)
// To write I use :
stream.write(reinterpret_cast<char*>(&x), sizeof(x));
2)手动缓冲:
// Initialization
const unsigned int length = 8192;
char buffer[length];
std::ofstream stream("test.dat", std::ios::binary | std::ios::trunc);
// Then I put manually the data in the buffer
// To write I use :
stream.write(buffer, length);
我预期同样的结果...
I expected the same result...
但是我的手动缓冲将性能提高了10倍,写入一个100MB的文件,与正常情况(没有重新定义缓冲区)相比,fstream缓冲没有任何改变。
But my manual buffering improve performance by a factor of 10 to write a file of 100MB, and the fstream buffering does not change anything compared to the normal situation (without redefining a buffer).
有人有这种情况的解释吗?
Does someone has an explanation of this situation ?
编辑:
这是新闻:一个基准只是在超级计算机位结构,持续intel Xeon 8核,Lustre文件系统和希望配置良好的编译器)
(我不解释1kB手动缓冲区的共振原因...)
EDIT : Here are the news : a benchmark just done on a supercomputer (linux 64-bit architecture, lasts intel Xeon 8-core, Lustre filesystem and ... hopefully well configured compilers) (and I don't explain the reason of the "resonance" for a 1kB manual buffer...)
编辑2:
和在1024 B的共鸣(如果有人有一个想法,我感兴趣):
EDIT 2 : And the resonance at 1024 B (if someone has an idea about that, I'm interested) :
推荐答案
这基本上是由于函数调用开销和间接。 ofstream :: write()方法继承自ostream。这个函数没有内联在libstdc ++中,这是第一个开销源。然后ostream :: write()必须调用rdbuf() - > sputn()来做实际写作,这是一个虚函数调用。
This is basically due to function call overhead and indirection. The ofstream::write() method is inherited from ostream. That function is not inlined in libstdc++, which is the first source of overhead. Then ostream::write() has to call rdbuf()->sputn() to do the actual writing, which is a virtual function call.
,libstdc ++将sputn()重定向到另一个虚函数xsputn(),它添加了另一个虚函数调用。
On top of that, libstdc++ redirects sputn() to another virtual function xsputn() which adds another virtual function call.
如果你自己把字符放入缓冲区, 。
If you put the characters into the buffer yourself, you can avoid that overhead.
这篇关于std :: fstream缓冲vs手动缓冲(为什么10倍增益与手动缓冲)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!