From: Robert Klemme Date: 2006-09-08T15:35:41+09:00 Subject: Re: Compiling Regexp only once --------------060104090601070606060308 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Rick DeNatale wrote: > On 9/4/06, Robert Klemme wrote: > >> Note though that it's usually faster to have a regexp in line. So in >> your case you might have a method that does the line parsing (or >> multiple line parsing) and that's where you can put the inline regexp >> for max efficiency. > > Robert, > > I'm not sure what you mean by inline, do you mean a regexp literal like > /[ABC]/ vs. RegEx.new('/[ABC]/') if so that's not clear. No, I meant that if /foo/ =~ something is faster than FOO = /foo/ ... if FOO =~ something Maybe the term "inline" was confusing. It refers to the fact that the RX is defined at the place where it matches. $ ruby rxbm.rb Rehearsal ---------------------------------------------------- inline match 0.781000 0.000000 0.781000 ( 0.781000) inline non match 1.500000 0.000000 1.500000 ( 1.500000) const match 0.969000 0.000000 0.969000 ( 0.985000) const non match 1.719000 0.000000 1.719000 ( 1.718000) ------------------------------------------- total: 4.969000sec user system total real inline match 0.797000 0.000000 0.797000 ( 0.797000) inline non match 1.484000 0.000000 1.484000 ( 1.485000) const match 1.000000 0.000000 1.000000 ( 1.000000) const non match 1.719000 0.000000 1.719000 ( 1.719000) (BM attached) Kind regards robert --------------060104090601070606060308 Content-Type: text/plain; name="rxbm.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="rxbm.rb" require 'benchmark' include Benchmark REP = 1_000_000 RX = /foo/ MATCH = ("foo" * 100).freeze NON_MATCH = ("fo" * 100).freeze bmbm do |b| b.report "inline match" do REP.times do /foo/ =~ MATCH end end b.report "inline non match" do REP.times do /foo/ =~ NON_MATCH end end b.report "const match" do REP.times do RX =~ MATCH end end b.report "const non match" do REP.times do RX =~ NON_MATCH end end end --------------060104090601070606060308--