[LLVMbugs] [Bug 21192] New: Reading from stdin is 10x slower than libstdc++

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Oct 7 10:50:38 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=21192

            Bug ID: 21192
           Summary: Reading from stdin is 10x slower than libstdc++
           Product: libc++
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: donovanhide at gmail.com
                CC: llvmbugs at cs.uiuc.edu, mclow.lists at gmail.com
    Classification: Unclassified

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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20141007/4e383297/attachment.html>


More information about the llvm-bugs mailing list