Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try using the scanf libraries:</p> <h3>r1.cpp</h3> <pre><code>&gt; cat r1.cpp #include &lt;iostream&gt; int main() { double x; long count = 0; while(std::cin &gt;&gt; x) { ++count; } std::cout &lt;&lt; count &lt;&lt; ": " &lt;&lt; x &lt;&lt; "\n"; } </code></pre> <h3>r2.cpp</h3> <pre><code>&gt; cat r2.cpp #include &lt;iostream&gt; #include &lt;stdio.h&gt; int main() { double x; long count = 0; while(fscanf(stdin, "%lf", &amp;x) == 1) { ++count; } std::cout &lt;&lt; count &lt;&lt; ": " &lt;&lt; x &lt;&lt; "\n"; } </code></pre> <h3>Results Serial</h3> <pre><code>&gt; g++ -O3 r1.cpp -o r1 &gt; time (cat t | ./r1) 40000000: 9.36e+08 real 0m57.669s user 0m56.992s sys 0m1.688s &gt; g++ -O3 r2.cpp -o r2 &gt; time (cat t | ./r2) 40000000: 9.36e+08 real 0m14.419s user 0m13.897s sys 0m1.352s </code></pre> <p>So it took onger than I expected about 60 seconds reading 40,000,000 numbers using IOstream. While only 15 seconds using scanf. So about 4 times faster.</p> <p>I did the same things but just writing the binary value of the doubles to the file.<br> Note you have to write them as binary and of course you loose all type safety and portability.</p> <pre><code>double x; std::cout.write((char*)&amp;x, sizeof(x)); </code></pre> <h3>r1b.cpp</h3> <pre><code>&gt; cat r1b.cpp #include &lt;iostream&gt; int main() { double x; long count = 0; while(std::cin.read((char*)&amp;x, sizeof(double))) { ++count; } std::cout &lt;&lt; count &lt;&lt; ": " &lt;&lt; x &lt;&lt; "\n"; } </code></pre> <h3>r2b.cpp</h3> <pre><code>&gt; cat r2b.cpp #include &lt;iostream&gt; #include &lt;stdio.h&gt; int main() { double x; long count = 0; while(fread(&amp;x, sizeof(double), 1, stdin) == 1) { ++count; } std::cout &lt;&lt; count &lt;&lt; ": " &lt;&lt; x &lt;&lt; "\n"; } </code></pre> <h3>Result binary</h3> <pre><code>&gt; time (cat t2 | ./r1b ) 40000000: 9.36e+08 real 0m3.930s user 0m3.592s sys 0m0.984s &gt; time (cat t2 | ./r2b ) 40000000: 9.36e+08 real 0m2.110s user 0m1.840s sys 0m0.804s </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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