From: Hugh Sasse Date: 2005-09-10T03:11:20+09:00 Subject: Re: Optimizing a single slow method On Sat, 10 Sep 2005, Eric Hodel wrote: > On 08 Sep 2005, at 20:46, Glenn M. Lewis wrote: > >> Hi! [...] >> Any ideas on how I can rewrite 'Contract.parseFile()' for >> speed? >> >> Thanks! >> -- Glenn Lewis >> >> def Contract.parseFile(file) >> return unless File.exists?(file) >> return if @@files.has_key?(file) I'd swap those two: test of a hash will be faster than test of a filesystem, so may as well bail out quickly. Do repeat keys happen often? >> @@files[file] = 1 Maybe it only needs to be a Set, not a Hash? Not sure how speeds compare. >> print "Parsing file #{file}..." >> File.open(file, "rb").each_line {|line| I think: >> line.chomp! >> # puts line >> fields = line.split(/,/) might be faster as fields = line.chomp.split(/,/) or if you only chomp the last field afterwards (shorter string to change)? >> next if fields.size < 8 Maybe line.count(",") first, and bail out quickly? Again, how often does this happen? >> datestring = fields[1] >> year = datestring[0..1].to_i >> month = datestring[2..3].to_i >> day = datestring[4..5].to_i reduce array refs: year, month, day = datestring[0..5].scan(/../).collect do |s| s.to_i end possibly That's all I can think of just now. Hugh