Skip to content

Reading from stdin is 10x slower than libstdc++ #21566

@llvmbot

Description

@llvmbot
Bugzilla Link 21192
Version unspecified
OS MacOS X
Reporter LLVM Bugzilla Contributor
CC @hfinkel,@hiraditya,@mclow

Extended Description

If I have a large file and want to read the contents line by line using iostream, the performance of libc++ is around 10x worse than that of libstdc++. Below is a comparison of a simple line counter for a binary compiled with g++ 4.9, clang 3.5 and the standard coreutils wc tool. More discussion here:

http://info.prelert.com/blog/stdgetline-is-the-poor-relation

Profiling seems to confirm that with libc++ a mutex is held for each single character read from stdin.

$cat wc.cc

#include <iostream>
#include <string>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::size_t i = 0;
  for (std::string line; std::getline(std::cin, line);) i++;
  std::cout << i << std::endl;
}

$ g++-4.9 --version
g++-4.9 (Homebrew gcc 4.9.1) 4.9.1
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++-4.9 -O3 wc.cc -o wc
$ time gzip -cd large_file.gz|head -n 100000 | ./wc
100000

real 0m2.811s
user 0m2.953s
sys 0m0.408s
$ c++ --version
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
$ c++ -O3 wc.cc -o wc
$ time gzip -cd large_file.gz|head -n 100000 | ./wc
100000

real 0m24.420s
user 0m27.114s
sys 0m0.322s
$ time gzip -cd large_file.gz|head -n 100000 | wc -l
100000

real 0m2.888s
user 0m3.229s
sys 0m0.304s

Metadata

Metadata

Assignees

Labels

bugzillaIssues migrated from bugzillalibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.performance

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions