From: "Ara.T.Howard" Date: 2005-07-02T12:05:56+09:00 Subject: Re: no clue On Sat, 2 Jul 2005, Daniel Brockman wrote: > Daniel Brockman writes: > >>> Also, this will get ran potentially thousands of times per second, >>> so executation speed is of some concern. >> >> I don't know if the above is the best you can do, but I do believe >> it is a bit faster than your original version. > > According to my tests, it is also more than twice as fast as that > enourmous strscan implementation. (Can anyone confirm?) sure it is. but with no error checking and it accepts invalid strings. it will also fail for things like 42.0 : value since '.' is not a \w (tricky). anyhow i didn't know the standard scan was so fast! a simple/similar version of the strscan method runs about the same for small strings, but scales a bit better: jib:~ > ruby a.rb HashString @ 16.7303600311279 HashStringSimple @ 21.1355850696564 jib:~ > cat a.rb require 'strscan' class HashString < ::Hash def initialize s ss = StringScanner::new s, false loop do ss.scan(%r/\s*([^:]*[^\s:])\s*:\s*([^,]*[^,\s])\s*,?\s*/o) or break self[ss[1]] = ss[2] end end end class HashStringSimple < ::Hash def initialize s s.scan(%r/\s*([^:]*[^\s:])\s*:\s*([^,]*[^,\s])\s*,?\s*/o){|k,v| self[k] = v} end end def time label fork do a = Time::now.to_f yield b = Time::now.to_f t = b - a puts "#{ label } @ #{ t }" end Process::wait end n = 2 ** 20 huge = '' n.times do |i| huge << "#{ rand } : #{ rand }" huge << ", " if i != n - 1 end time('HashString'){ hs = HashString::new huge } time('HashStringSimple'){ hs = HashStringSimple::new huge } cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================