【问题标题】:c++ ostringstream improve IO performance?c++ ostringstream 提高 IO 性能?
【发布时间】:2016-09-25 10:53:10
【问题描述】:

当我尝试将一些字符串写入文件时,我注意到使用 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&lt;&lt; 都会是一个 IO?
而且,我们可以将其推广以提高从文件读取的性能吗?

【问题讨论】:

    标签: c++ file io std


    【解决方案1】:

    第二种方法由于 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;
    }
    

    在我的系统上使用 g++ -std=c++14 -O3 编译的结果:

    0.025744
    0.0173609
    

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2016-11-09
      • 2014-06-11
      相关资源
      最近更新 更多