2 ms·
Counting words in Python, Go, C++, C, Awk, Forth, and Rust (2021)
- deleted 2y ago[deleted]
- librasteve 2y agoI 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'.
- frizlab 2y agoI 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)…
- Kon-Peki 2y agoThis 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?
- frizlab 2y agoI took the CPU difference into account by comparing w/ the rest of the bench that I ran locally. The 2021 is probably the answer!