From: Paul Brannan Date: 2007-09-24T22:50:00+09:00 Subject: Re: I am new to Ruby and I could use some expert advice as to how I can make this code run faster. Since you didn't provide python code for us to compare against, I can only assume that the code to which you refer is from this thread: http://groups.google.com/group/comp.lang.python/browse_thread/thread/fa33727aa3a6751f/7efd9f93b7834a23?lnk=st&q=python+scramble&rnum=4#7efd9f93b7834a23 I don't know which version of the code you used, but here's a version using String#tr which is equivalent to Michael Spencer's version using translate: def scramble_tr(fname) f = File.new(fname, "rb") _fname = fname + ".scrambled" begin File.exist?(_fname) if File.delete(_fname) rescue end ff = File.new(_fname, "wb+") for l in f l.tr!("\x0-\x7f", "\x80-\xff") ff.write(l) end f.close() ff.close() end Since I'm on a much slower machine, I used a 1MB file: cout@bean:~/tmp$ ruby test5.rb testfile user system total real scramble 10.780000 0.120000 10.900000 ( 11.514982) _scramble 11.630000 0.050000 11.680000 ( 11.918237) scramble_tr 0.250000 0.030000 0.280000 ( 0.416945) If you're going to do a comparison, please make sure you aren't comparing apples to oranges. The language shootout isn't perfect, but it does a pretty good job: http://shootout.alioth.debian.org/ Hope this helps, Paul