From: Alan Burch Date: 2006-03-11T07:57:47+09:00 Subject: Help me understand why the Ruby block is slower than without I just wrote my first Ruby script. I'm an experienced C and perl programmer, so please, if it looks too much like these languages and not Ruby, let me know. I've got a 100K word list (Linux dictionary) on my Mac and am opening it then looking for any words that are exactly 10 letters long with no letters repeating ('profligate\n') == 11 is a match. After I wrote my first version I did some playing. I first saw that the array class mixed in enumerable and that I could use the to_a call from there, but a quick check using -r profile showed that my original call to split was a much quicker way to convert from a string to an array. I then tried putting the File.open in a block and found that this was much slower, even if I subtract out the time for the open, which I assume is an error in how the profile is counting total time. Here's the faster version: f = File.open("./words") begin while f.gets if $_.length == 11 ar = $_.split(//) if ar.uniq! == nil print "#{ar.to_s}" end end end rescue EOFError f.close end And here's the slower block version: File.open("./words") { |f| while f.gets if $_.length == 11 ar = $_.split(//) if ar.uniq! == nil print "#{ar.to_s}" end end end } Again, the words file is just a list of about 100K unique words from the dict command or similar on *nix.... Any critique welcome and enlightenment is encouraged. Thanks! -- Posted via http://www.ruby-forum.com/.