clang++ fstreams 10X slower than g++

只谈情不闲聊 提交于 2019-12-04 04:10:13

问题


Q: Is there a way to speed up clang++ STD Library fstreams? (And does anybody know why it is so much slower than g++?)

I am trying to process very large (many GBs) binary data files and was surprised to find the performance was so poor. At first, I thought it was something to do with my code. But I am seeing the same slow performance in a boiled down example.

I even tried allocating different size buffers via rdbuf()->pubsetbuf() but this didn't seem to have much effect.

Here is a simple input/output example:

#include <fstream>

int main() {
    std::ifstream is {"bigSourceFile"};
    std::ofstream os {"bigSourceFileCopy"};

    std::string line;
    while (std::getline (is, line) ) {
        os << line;
    }
}

Here is some code to generate a 1.3GB source file. Use this to generate the source file for the readWrite program:

#include <fstream>
#include <string>

std::string createTailStr () {
    std::string result {"__"};
    for (auto i (0); i< 58; ++i) {
        result += 'A'+i;
    }

    return result;
}

int main() {

    std::string tail {createTailStr()};

    std::ofstream os {"bigSourceFile"};

    constexpr auto Lines (20000000ul);
    for (auto i (0); i < Lines; ++i) {
        os << i << tail << '\n';
    }
}

On my machine (running OSX El Capitan and the latest Xcode): the example (the first code listing) takes aprox. 50 seconds to run compiling from the command line:

clang++ -std=c++11 -o readWrite readWrite.cpp

However, when compiling against g++ and its libstdc++, the program only takes about 5 seconds.

I even built the latest cut of llvm stdc++ from llvm.org and that build was even slower (aprox 90 seconds).

To sum up: clang++ : 50 seconds; g++ : 5 seconds

来源:https://stackoverflow.com/questions/38624468/clang-fstreams-10x-slower-than-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!