[英]c++ ostringstream improve IO performance?
When I try to write some strings to a file, I notice that using ostringstream can improve the performance. 当我尝试将一些字符串写入文件时,我注意到使用ostringstream可以提高性能。
The code below do the following things: 下面的代码执行以下操作:
1. generate some random string 1.生成一些随机字符串
2. write it to a file using ostringstream 2.使用ostringstream将其写入文件
3. write it to a file using ofstream 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;
}
On my computer, ostringstream uses 0.016933 second, but ofstream uses 0.132003 second 在我的计算机上,ostringstream使用0.016933秒,而ofstream使用0.132003秒
I don't know why this happens? 我不知道为什么会这样?
Is it because using ostringstream reduces the number of IO? 是否因为使用ostringstream减少了IO数量?
Does std::ofstream has a buffer to reduce the number of IO? std :: ofstream是否具有缓冲区以减少IO数量? or every time we use fout<<
it will be an IO? 还是每次使用fout<<
都会是IO?
And, can we generalize this to improve the performance of reading from file? 而且,我们可以对此进行概括以提高从文件读取的性能吗?
The second method defeats internal buffering due to std::endl (which is putting the newline character and flushes the stream). 第二种方法由于std :: endl(放置换行符并刷新流)而使内部缓冲失效。
By replacing std::endl with \\n
and flushing the stream after writing all data, the second method becomes faster than the first (The string stream becomes an additional overhead). 通过用\\n
替换std :: endl并在写入所有数据后刷新流,第二种方法变得比第一种更快(字符串流成为额外的开销)。
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;
}
Result on my system compiled with g++ -std=c++14 -O3: 使用g ++ -std = c ++ 14 -O3编译的系统上的结果:
0.025744
0.0173609
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.