From: Joe Van Dyk Date: 2005-08-14T03:26:47+09:00 Subject: Re: no clue On 8/13/05, Simon Kr�ger wrote: > Hi Joe, > > as a more serious contribution to your problem: > this is 10 times faster: > > data.each{ |j, line| > k, v = -2, 0 > while (v = line.index(58, k)) > h5[j][line[(k+2)...v].intern] = > line[(v+2)...(k = line.index(44, v) || line.length)] > end > } > > i attached the whole test script, the output is: > > user system total real > inject 4.640000 0.046000 4.686000 ( 4.687000) > scan 5.204000 0.063000 5.267000 ( 5.875000) > eval 6.078000 0.016000 6.094000 ( 6.094000) > tmp 4.375000 0.000000 4.375000 ( 4.391000) > index 0.312000 0.000000 0.312000 ( 0.312000) > true > > cheers > > Simon > > > require 'benchmark' > > a = [] > 100.times {|i| a << [i, "x_position: #{200+i}, y_position: #{400+i}, z_position: #{300+i}"]} > > data = a > iter = 1000 > h1 = h2 = h3 = h4 = h5 = {} > > Benchmark.bm 20 do |bm| > bm.report("inject") do > iter.times { > data.each{|i, line| h1[i]={}; line.split(/\s*[:,]\s*/).inject(false){|a,b| > if a then h1[i][a.to_sym]=b; a=false else a=b end }} > } > end > > bm.report "scan" do > iter.times { > data.each{|i, line|h2[i]={}; line.scan(/(\w+)\s*:\s*([^, ]*)/) do |k, v| > h2[i][k.to_sym] = v > end > } } > end > > bm.report "eval" do > iter.times { > h3 = eval('{' << ('~' << data.join('~')). > gsub!(/\s*([^:~]+):\s*([^,~]+),?/, ':\1=>\'\2\','). > gsub!(/~(\d+)~/, '},\1=>{')[2..-2] << '}}') > } > end > > bm.report "tmp" do > iter.times { > data.each{ |j, line| h4[j]={};tmp = line.split(/\s*[:,]\s*/) > (0...tmp.size).step(2){ |i| h4[j][tmp[i].to_sym]=tmp[i+1] } > }} > end > > bm.report "index" do > iter.times { > data.each{ |j, line| > k, v = -2, 0 > while (v = line.index(58, k)) > h5[j][line[(k+2)...v].intern] = line[(v+2)...(k = line.index(44, v) || line.length)] > end > } > } > end > > end > > p((h1 == h2) && (h1 == h3) && (h1 == h4) && (h1 == h5)) Thank you! I must say that the logic in the 'index' one confuses me though.