A very nice (longish but enjoyable) read this morn...
# linking-together
s
A very nice (longish but enjoyable) read this morning. Performance comparison: counting words in Python, C/C++, Awk, Rust, and more - https://benhoyt.com/writings/count-words/
j
N.B. the "optimized" awk example is just the normal way to do it in awk. 🙂 I'm pleasantly surprised by the performance of the Java version. OTOH, the Common Lisp implementation looks like it was written by someone who doesn't know very much CL. 🤷🏻‍♂️
👍 1
t
that's one of the problem with contrived examples for benchmarks, the implementation quality is disputed, as is the task for comparison
💯 2
j
That, and very few real programs spend all their time doing <whatever microbenchmark>.
w
For benchmarking this is not a good task. As the kind of thing I write all the time, the start of an interview for fresh grads, it's not so bad. Let's see now. Hmm, this Ruby smells...
Copy code
counts = Hash.new(0)
STDIN.each_line { |line| line.downcase.split.each { |w| counts[w] += 1 }}
counts.sort_by { |k,v| -v }.each { |k,v| puts "#{k} #{v}" }
old. A quick round of golf:
Copy code
STDIN.flat_map{_1.downcase.split}.
  tally.sort_by{-_2}.
  each{puts _1.join(" ")}
It only goes to show that counting things is a common Rubyism. Games like this always remind me of the The Evolution of a Haskell Programmer https://pages.cpsc.ucalgary.ca/~robin/class/449/Evolution.htm.
t
cat file.txt | wc -w
🤣 1
I suppose they are counting how many times each word appears...
j