C++ still isn't cutting it
FWIW, the Nim version is (no deps besides stdlib):<p><pre><code>  import os, strutils, tables, heapqueue

  iterator topByVal*[K, V](c: Table[K, V], n = 10, min = V.low): (K, V) =
    var q = initHeapQueue[(V, K)]()
    for key, val in c:
      if val &gt;= min:
        let elem = (val, key)
        if q.len &lt; n: q.push(elem)
        elif elem &gt; q[0]: discard q.replace(elem)
    var y: (K, V)         # yielded tuple
    while q.len &gt; 0:      # q now has top n entries
      let r = q.pop
      y[0] = r[1]
      y[1] = r[0]
      yield y             # yield in ASCENDING order

  template maybeCount(count, word: untyped) =
    if word.len &gt; 1: count.mgetOrPut(word, 0).inc

  proc main() =
    var count = initTable[string, uint]()
    for path in &quot;.&quot;.walkDirRec:
      if not path.endsWith(&quot;.txt&quot;):
        continue
      for line in path.lines:
        var word = &quot;&quot;
        for c in line.toLower:
          if c in {&#x27;a&#x27;..&#x27;z&#x27;}:
            word.add c
          else:
            count.maybeCount word
            if word.len &gt; 0: word.setLen 0
        count.maybeCount word # line ends in a word
    for word, cnt in count.topByVal:
      echo cnt, &quot; &quot;, word

  main()
</code></pre>
Doing a gcc-10 profile-guided-optimization build on this, I get the min time of 5 trials running on Charles Dickens&#x27; Tale Of Two Cities { opening paragraph is very optimization claims relevant! :-) } available here: <a href="http:&#x2F;&#x2F;www.textfiles.com&#x2F;etext&#x2F;AUTHORS&#x2F;DICKENS&#x2F;dickens-tale-126.txt" rel="nofollow">http:&#x2F;&#x2F;www.textfiles.com&#x2F;etext&#x2F;AUTHORS&#x2F;DICKENS&#x2F;dickens-tale-...</a><p><pre><code>   7.3 ms  EDIT- tptacek&#x27;s C version (w&#x2F;bug-fix &amp; new tokenization)
  15.8 ms  Above Nim 1.4 PGO --gc:arc
  24.8 ms  Above Nim 1.4 --gc:arc
  27.3 ms  Original C++ PGO
  30.2 ms  Original C++ -O3 only
  42.6 ms  dga&#x27;s Rust with rustc-1.47.0 build --release
</code></pre>
This is on a i7-6700k.  So, Skylake core and probably more importantly 8 MiB L3.  So, also FWIW, I could not reproduce the Rust&#x2F;C++ timing ratios with the above mentioned input file.  I would also point out that not only is globwalk unnecessary as @burntsushi points out, neither are regexes nor even sorting.  A heap is a better way to do &quot;top N&quot;, but you may need to reverse the answer if you care.  I mean, maybe the point of the original C++ is to also bench&#x2F;exhibit use of regex engines and part of the Rust point is utf8 or some such, but to me that seems more like a distraction.<p>Anyway, maybe my rust build environment is screwy.  Hence, my inclusion of an exact input .txt file for others to try out if they care.  I had to add some extern crates and [dependencies] of anyhow, lazysort, regex, globwalk.  I&#x27;ve never done a PGO build with rust, either.  So, that may help.