Hacker News new | past | comments | ask | show | jobs | submit login
Counting words in Python, Go, C++, C, Awk, Forth, and Rust (2021) (benhoyt.com)
8 points by tosh 7 months ago | hide | past | favorite | 4 comments



I know that this is not a golf match, nevertheless, with the OP "this is what I would write in Python in real life" (eliding the module import) being:

  counts = collections.Counter()
  for line in sys.stdin:
    words = line.lower().split()
    counts.update(words)

  for word, count in counts.most_common():
    print(word, count)

I can't but resist to say what I would write in raku in "real life" (all in standard lib btw)...

  sub MAIN($in) {
    say $in.words>>.lc.Bag.sort(-*.value);
  }

This is Unicode safe and (imho) gives a good reflection that (i) raku is very good for whipping up string oriented 'one-liners' in functional stylee, (ii) that built in Set datatypes such as Bag are quite handy and (iii) that often a one-liner is better for expressing the coders' intent than a prosaic multi-line 'soup'.


I don’t know how the Swift program was compiled, but on my computer I get 2.64 (w/ normalization of my actual results to take CPU diff into account)…


This article is from 2021 - before they started the Foundation rewrite.

If you are using an up-to-date Swift compiler, it has at least some of that rewrite code. Also, your computer may be faster than his?


I took the CPU difference into account by comparing w/ the rest of the bench that I ran locally.

The 2021 is probably the answer!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: