c++ ostringstream改进IO性能
c++ ostringstream improve IO performance?
当我尝试将一些字符串写入文件时,我注意到使用ostringstream可以提高性能。
下面的代码做了以下事情:
1. 生成一些随机字符串
2. 使用ostringstream
将其写入文件3.使用ofstream
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <sys/time.h>
using namespace std;
double timeGetTimeOfDay(){
struct timeval t;
gettimeofday(&t, NULL);
return double(t.tv_sec) + double(t.tv_usec) / 1000000;
}
string genRandString(int length){
string r;
for(int i=0; i<length; i++)
r.push_back(char('a' + rand()%26));
return r;
}
void genRandStrings(vector<string>& allStrings, int count, int length){
srand(unsigned(time(NULL)));
for(int i=0; i<count; i++)
allStrings.push_back(genRandString(length));
}
int main(){
ofstream fout("temp");
vector<string> allStrings;
genRandStrings(allStrings, 100000, 100);
// output method1
double start = timeGetTimeOfDay();
ostringstream os;
for(int i=0; i<allStrings.size(); i++)
os<<allStrings[i]<<endl;
fout<<os.str();
double end = timeGetTimeOfDay();
cout<<end - start<<endl;
// output method2
start = timeGetTimeOfDay();
for(int i=0; i<allStrings.size(); i++)
fout<<allStrings[i]<<endl;
end = timeGetTimeOfDay();
cout<<end - start<<endl;
fout.close();
return 0;
}
在我的计算机上,ostringstream使用0.016933秒,但ofstream使用0.132003秒
我不知道为什么会这样?
是因为使用ostringstream减少了IO的数量吗?
是否std::ofstream有一个缓冲区来减少IO的数量?或者每次使用fout<<
都是IO?
那么,我们是否可以将其推广到提高从文件读取的性能呢?
第二个方法由于std::endl(它会放入换行符并刷新流)而失败了内部缓冲。
通过将std::endl替换为n
并在写入所有数据后刷新流,第二个方法比第一个方法更快(字符串流成为额外的开销)。
int main(){
vector<string> allStrings;
genRandStrings(allStrings, 100000, 100);
// output method1
{
ofstream fout("temp1"); // Distinct output file
double start = timeGetTimeOfDay();
ostringstream os;
for(unsigned i=0; i<allStrings.size(); i++)
os<<allStrings[i]<<'n'; // New line, only
fout << os.str();
fout.flush(); // Flushing output
double end = timeGetTimeOfDay();
cout<<end - start<<endl;
}
// output method2
{
ofstream fout("temp2"); // Distinct output file
double start = timeGetTimeOfDay();
for(unsigned i=0; i<allStrings.size(); i++)
fout<<allStrings[i]<<'n'; // New line, only
fout.flush(); // Flushing output
double end = timeGetTimeOfDay();
cout<<end - start<<endl;
}
return 0;
}
使用c++ -std=c++14 -O3编译的系统上的结果:
0.025744
0.0173609
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- Seg Fault Issue C++ (file IO / getline)
- OpenMP阵列性能较差
- 递归列出所有目录中的C++与Python与Ruby的性能
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 为什么在读取文件大小时文件IO速度会发生变化
- 大小相等但成员数量不同的结构之间的性能差异
- 为什么constexpr的性能比正常表达式差
- 在类中使用随机生成器时出现性能问题
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 海湾合作委员会 ARM 性能下降
- GCC 和 Clang 代码性能的巨大差异
- 在容量内调整矢量大小时的性能影响
- Agora.io 虚幻引擎插件构建错误
- 了解算法的性能差异(如果以不同的编程语言实现)
- 未达到的情况会影响开关外壳性能
- Windows:基于事件的重叠 IO 与 IO 完成端口,实际性能
- C和C++风格文件IO的性能差异
- Linux异步IO与libaio性能问题
- c++ ostringstream改进IO性能