Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::cin really slow
    primarykey
    data
    text
    <p>So I was trying to write myself a command for a linux pipeline. Think of it as a replica of gnu 'cat' or 'sed', that takes input from stdin, does some processing and writes to stdout.</p> <p>I originally wrote an AWK script but wanted more performance so I used the following c++ code:</p> <pre><code>std::string crtLine; crtLine.reserve(1000); while (true) { std::getline(std::cin, crtLine); if (!std::cin) // failbit (EOF immediately found) or badbit (I/O error) break; std::cout &lt;&lt; crtLine &lt;&lt; "\n"; } </code></pre> <p>This is exactly what cat (without any parameters does). As it turns out, this program is about as slow as its awk counterpart, and nowhere near as fast as cat.</p> <p>Testing on a 1GB file:</p> <pre><code>$time cat 'file' | cat | wc -l real 0m0.771s $time cat 'file' | filter-range.sh | wc -l real 0m44.267s </code></pre> <p>Instead of getline(istream, string) I tried cin.getline(buffer, size) but no improvements. This is embarassing, is it a buffering issue? I also tried fetching 100KB at a time instead of just one line, no help! Any ideas?</p> <p>EDIT: What you folks say makes sense, BUT the culprit is not string building/copying and neither is scanning for newlines. (And neither is the size of the buffer). Take a look at these 2 programs:</p> <pre><code>char buf[200]; while (fgets(buf, 200, stdin)) std::cout &lt;&lt; buf; $time cat 'file' | ./FilterRange &gt; /dev/null real 0m3.276s char buf[200]; while (std::cin.getline(buf, 200)) std::cout &lt;&lt; buf &lt;&lt; "\n"; $time cat 'file' | ./FilterRange &gt; /dev/null real 0m55.031s </code></pre> <p>Neither of them manipulate strings and both of them do newline scanning, however one is 17 times slower than the other. They differ only by the use of cin. I think we can safely conclude that cin screws up the timing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload