From: Robert Dober Date: 2008-07-31T07:12:09+09:00 Subject: Re: Suggestions for improving a trivial tag parser On Wed, Jul 30, 2008 at 10:43 PM, Gregory Brown wrote: > It does a double pass through the segments rather than a single pass, Yes but these two passes are quite fast, see below. > However, I think it'll be okay for my purposes (PDF inline styling), > unless I missed some other concern Rolando had. Well trying to be useful I checked for some larger texts, I omitted the conditional #first at the end of the parsing method for clarity. Turns out that split is still much better than scanning for a string of a size over one megabyte, but it is not really fast either: 539/39 > cat split.rb && ruby split.rb require 'benchmark' def split_select txt txt.split(%r{()}).select{|x| ! x.empty? } end def split_delete txt txt.split(%r{()}).delete_if{|x| x.empty? } end def use_scan txt r=[] txt.scan(%r{(.*?)()}) do | pr,po | r << pr unless pr.empty?; r << po end r << $' unless $'.empty? end N = 400_000; text = "bolditalicboldboldnormal" * N; Benchmark.bmbm do | bm | bm.report("split_select") do split_select text end bm.report("split_delete") do split_delete text end bm.report("use_scan") do use_scan text end end Rehearsal ------------------------------------------------ split_select 6.844000 0.094000 6.938000 ( 7.063000) split_delete 7.687000 0.109000 7.796000 ( 7.953000) use_scan 20.063000 0.203000 20.266000 ( 20.109000) -------------------------------------- total: 35.000000sec user system total real split_select 6.344000 0.031000 6.375000 ( 6.485000) split_delete 6.500000 0.109000 6.609000 ( 6.359000) use_scan 16.625000 0.265000 16.890000 ( 16.906000) HTH Robert -- http://ruby-smalltalk.blogspot.com/ There's no one thing that's true. It's all true. -- Ernest Hemingway