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
fstreams
are not part of theSTL
which only refers to thealgorithms
,containers
anditerators
elements. – Gooseclang++ -stdlib=libstdc++
– Receivetime taken: 14 seconds
– Roanneg++
,gcc
,clang++
andclang
all seem to map to the same clang compiler:Apple LLVM version 7.3.0 (clang-703.0.29)
, which is inInstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
. – Mairamaire<<
and>>
are doing in your code? – Fleeta