<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Reading from stdin is 10x slower than libstdc++"
   href="http://llvm.org/bugs/show_bug.cgi?id=21192">21192</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Reading from stdin is 10x slower than libstdc++
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>donovanhide@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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:

<a href="http://info.prelert.com/blog/stdgetline-is-the-poor-relation">http://info.prelert.com/blog/stdgetline-is-the-poor-relation</a>

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</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>