From: James Edward Gray II Date: 2006-02-13T03:30:25+09:00 Subject: Re: Cutting a piece of text On Feb 12, 2006, at 12:08 PM, Marcin Miel��y��ski wrote: > James Edward Gray II wrote: > >> >> " Lalalalala > lyrics>".sub(/<(\w+)[^>]+>/, "<\\1>") >> => " Lalalalala " > > reluctant would a bit faster: > > p " Lalalalala lyrics>".gsub(/<(\w+).*?>/, "<\\1>") Are you sure? $ ruby regexp_time.rb Rehearsal ------------------------------------------------- /<(w+)[^>]+>/ 7.210000 0.030000 7.240000 ( 7.266166) /<(w+).*?>/ 7.710000 0.020000 7.730000 ( 7.757304) --------------------------------------- total: 14.970000sec user system total real /<(w+)[^>]+>/ 7.170000 0.030000 7.200000 ( 7.227075) /<(w+).*?>/ 7.730000 0.020000 7.750000 ( 7.777196) $ cat regexp_time.rb #!/usr/local/bin/ruby -w require "benchmark" tests = 1000000 data = " Lalalalala " Benchmark.bmbm do |x| x.report("/<(\w+)[^>]+>/") do tests.times { data.sub(/<(\w+)[^>]+>/, "<\\1>") } end x.report("/<(\w+).*?>/") do tests.times { data.sub(/<(\w+).*?>/, "<\\1>") } end end __END__ ;) James Edward Gray II