From: Bob Hutchison Date: 2007-08-03T22:00:29+09:00 Subject: Re: Fast implementation of XML escape Hi Robert, Thanks for the response. On 3-Aug-07, at 8:47 AM, Robert Klemme wrote: > > You are on the right track. There is just one thing to improve: get > rid of "case": > > class Converter > MAP = { > "&" => "&", > # ... > } > > def self.convert(s) > s.gsub(/[&<>'"]/) do |m| > MAP[m] || "ERROR" > end > end > end My version3 was: @@convert = { '&' => '&', '<' => '<', '>' => '>', "'" => ''', '"' => '"' } def version3(input) # all kinds of other processing of input simulated by the input.dup result = input.dup result.gsub!(/[&<>'"]/) do | match | @@convert[match] end return result end which is pretty much what you suggested, but it takes 29.71 seconds (rather than 8.74 seconds). > > Also, I believe x.dup.gsub! is less efficient than doing just a > single x.gsub. That dup is just so I could 1) simulate some extra work that I had to do; and 2) make sure I didn't permanently change the test string with the gsub!. Thanks! Cheers, Bob > > Kind regards > > robert > ---- Bob Hutchison -- tumblelog at http:// www.recursive.ca/so/ Recursive Design Inc. -- weblog at http://www.recursive.ca/ hutch http://www.recursive.ca/ -- works on http://www.raconteur.info/ cms-for-static-content/home/