Performance comparison: counting words in Python, Go, C++, C, Awk, Forth, Rust
I was suprised at the C++ outcome, so I started testing myself. I was very amazed to find a 4~5x speedup when reading directly from file (std::ifstream) instead of reading stdin. This was on a 10.5.7 MBP 2014<p>[edit]<p>more like 9~10x actually...<p><pre><code>  time { ./build/Release/countwords_ifstream; } | tail
  ...
  real 0m1.337s
  user 0m1.332s
  sys 0m0.022s

  time { ./build/Release/countwords_original < kjvbible_x10.txt; } | tail
  ...
  real 0m12.184s
  user 0m12.138s
  sys 0m0.044s
</code></pre>
changes between these two:<p><pre><code>  6a7
  > #include <fstream>
  8a10
  >  std::ifstream inp( "/Users/macgyverismo/Desktop/test/kjvbible_x10.txt" );
  13c15
  <     while (std::cin >> word) {
  ---
  >     while (inp >> word) {</code></pre>