From: David Alan Black Date: 2000-12-16T10:21:21+09:00 Subject: [ruby-talk:7367] Re: Ruby performance question On Sat, 16 Dec 2000, Dave Thomas wrote: > OK, this is tacky, and probably not worth it, but it knocks about 15% > of the run times on my box, using an 80,000 line input file with 6 > key/value pairs per line. The idea is simply to avoid creating the > intermediate object that contains a single key-value which is then > split across the '='. Instead, we assume the input is well-formed and > split into the final strings directly. > > > h = Hash.new > > while line = gets > a = line.chomp!.tr!('=', ',').split(',') > 0.step(a.size-2, 2) do |i| > h[a[i]] = a[i+1] (Oh, for Array#to_h! Wouldn't: "h = line.to_h" be nice?) If the lines are guaranteed to be well-formed, you could also do: while line = gets record = line.chomp!.split(/[,=]/) h[record.shift] = record.shift until record.empty? h.clear end which bypasses the tr! phase. And seems to pick up a bit of time -- a quick benchmarking of the three versions, on a file of 20000 lines (all right, I'm impatient :-) with six key-value pairs per line, looks like this: user system total real 13.200000 0.030000 13.230000 ( 13.225895) # Eric 11.200000 0.050000 11.250000 ( 11.242669) # Dave 9.870000 0.040000 9.910000 ( 9.918286) # David I wanted to use the reverse/pop (instead of shift) technique that Brian Feldman had suggested for interleaving, but I couldn't figure out how to get at the key before the value. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav