News for hackers. Stuff that matters.
Re:Performance comparison: counting words in Python, Go, C++... by bestinterest on Monday March 15, 2021 @10:55AM (#26466744)
Simple Ruby implementation

  counts = Hash.new(0)
  STDIN.each_line do |line|
    words = line.downcase.split
    words.each do |word|
      counts[word] += 1
    end
  end

  counts.sort_by { |_k, v| -v }.each do |word, count|
    p "#{word} #{count}"
  end
Re:Performance comparison: counting words in Python, Go, C++... by petercooper on Friday March 19, 2021 @09:52AM (#26513315)
Alternative approach depending on preferred readability:

    STDIN.each
         .map(&:downcase)
         .map(&:split)
         .flatten
         .tally
         .sort_by { _2 }
         .reverse
         .each { |word, count| puts "#{word} #{count}" }
(It has a slightly different performance profile and is slightly slower than the other implementation. 477ms vs 418ms on the KJV dataset for me.)
Re:Performance comparison: counting words in Python, Go, C++... by frankenst1 on Monday March 15, 2021 @11:01AM (#26466813)
Could you update it so that you have "word count\n" on each line?
Re:Performance comparison: counting words in Python, Go, C++... by bestinterest on Monday March 15, 2021 @11:23AM (#26467078)
Like that?
Re:Performance comparison: counting words in Python, Go, C++... by frankenst1 on Monday March 15, 2021 @11:58AM (#26467567)
Yep, this is probably the most readable implementation of all languages so far. I ran it and got 3.59s (Go 0.82s, Rust 1.55s, Shell 42.6s(?))

For accurate diff results, one would need `puts` instead of `p` to get rid of the additional quotes.

Re:Performance comparison: counting words in Python, Go, C++... by bestinterest on Monday March 15, 2021 @12:29PM (#26468066)
Checkout Crystal lang one (Ruby but fast), https://github.com/benhoyt/countwords/pull/21/files seems to keep readability intact and committer is saying it outperforms Go which is interesting.
Re:Performance comparison: counting words in Python, Go, C++... by frankenst1 on Monday March 15, 2021 @12:52PM (#26468388)
Clocks in at 1.48s for me which is faster than Ruby but slower than JavaScript.
Re:Performance comparison: counting words in Python, Go, C++... by derjames on Monday March 15, 2021 @10:14PM (#26472094)
with --no-debug --release ?
Re:Performance comparison: counting words in Python, Go, C++... by frankenst1 on Tuesday March 16, 2021 @09:36AM (#26476483)

    crystal build --no-debug --release simple.cr -o simple-cr