David Alan Black <dblack / candle.superlink.net> writes: > For what it's worth, here's a tweaked/hacked version of your code. > Some of the changes are things mentioned above; others are just little > experiments, some of which didn't seem to matter much for speed. I did a little tweaking too. It seemed to me that a lot of effort was going to the the final output loop, testing to see if an entry had two or more words in it, where perhaps only 1% of them do. So, I added another hash which records the keys of known anagrams: def fa6(words, out = STDOUT) anagrams = {} keys = {} word, key = nil for word in words do word.chomp! word.downcase! key = word.split('') key.sort! key = key.join if anagrams[key] anagrams[key] << word keys[key] = 1 else anagrams[key] = [ word ] end end for key in keys.keys out.puts anagrams[key].join(' ') end end This benchmarks slightly faster than David's latest: dave[~/tmp 0:25:05] head -20000 /usr/dict/words | ruby -w anag.rb user system total real original 5.060000 0.090000 5.150000 ( 5.149464) omit to_s 2.700000 0.000000 2.700000 ( 2.709957) block var 2.230000 0.010000 2.240000 ( 2.238881) lst[1] 2.220000 0.010000 2.230000 ( 2.247656) sort! 2.140000 0.010000 2.150000 ( 2.165254) david 1.540000 0.000000 1.540000 ( 1.541778) keys 1.400000 0.000000 1.400000 ( 1.398975) Next! Dave