How fast are Linux pipes anyway?
For some reason, this raised my curiosity how fast different languages write individual characters to a pipe:<p>PHP comes in at about 900KiB&#x2F;s:<p><pre><code>    php -r &#x27;while (1) echo 1;&#x27; | pv &gt; &#x2F;dev&#x2F;null
</code></pre>
Python is about 50% faster at about 1.5MiB&#x2F;s:<p><pre><code>    python3 -c &#x27;while (1): print (1, end=&quot;&quot;)&#x27; | pv &gt; &#x2F;dev&#x2F;null
</code></pre>
Javascript is slowest at around 200KiB&#x2F;s:<p><pre><code>    node -e &#x27;while (1) process.stdout.write(&quot;1&quot;);&#x27; | pv &gt; &#x2F;dev&#x2F;null
</code></pre>
What&#x27;s also interesting is that node crashes after about a minute:<p><pre><code>    FATAL ERROR: Ineffective mark-compacts
    near heap limit Allocation failed -
    JavaScript heap out of memory
</code></pre>
All results from within a Debian 10 docker container with the default repo versions of PHP, Python and Node.<p>Update:<p>Checking with strace shows that Python caches the output:<p><pre><code>    strace python3 -c &#x27;while (1): print (1, end=&quot;&quot;)&#x27; | pv &gt; &#x2F;dev&#x2F;null
</code></pre>
Outputs a series of:<p><pre><code>    write(1, &quot;11111111111111111111111111111111&quot;..., 8193) = 8193
</code></pre>
PHP and JS do not.<p>So the Python equivalent would be:<p><pre><code>    python3 -c &#x27;while (1): print (1, end=&quot;&quot;, flush=True)&#x27; | pv &gt; &#x2F;dev&#x2F;null
</code></pre>
Which makes it compareable to the speed of JS.<p>Interesting, that PHP is over 4x faster than the Python and JS.