On 18.03.2009 19:12, Eleanor McHugh wrote:
> On 18 Mar 2009, at 16:25, Robert Klemme wrote:
>> 2009/3/17 Eleanor McHugh <eleanor / games-with-brains.com>:
>>>        spaces = /\s+/
>>>        chars = /[^A-Za-z0-9\s]/
>> This is generally less efficient and AFAIK there are no memory leaks
>> attached to this.  It's a different story with varying regular
>> expressions (i.e. based on input or configuration).  But in this case
>> I'd leave them in place.
> 
> Interesting, I'll have to do some tests to see what the difference is  
> in practice as this is a code pattern I use a lot.

It seems, the difference has decreased with 1.9, but you can at least 
say that it's not slower to have a regular expression inline:

allruby rx.rb
ruby 1.8.5 (2006-08-25) [i386-linux]
Rehearsal -------------------------------------------------------
inline               15.300000   4.000000  19.300000 ( 19.354039)
separate             15.440000   3.940000  19.380000 ( 19.426682)
out of the loop      15.370000   4.000000  19.370000 ( 19.423543)
--------------------------------------------- total: 58.050000sec

                           user     system      total        real
inline               15.330000   3.990000  19.320000 ( 19.341197)
separate             15.400000   3.980000  19.380000 ( 19.426264)
out of the loop      15.410000   3.950000  19.360000 ( 19.426081)
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]
Rehearsal -------------------------------------------------------
inline                4.470000   0.000000   4.470000 (  4.502066)
separate              4.450000   0.000000   4.450000 (  4.466547)
out of the loop       4.460000   0.000000   4.460000 (  4.473198)
--------------------------------------------- total: 13.380000sec

                           user     system      total        real
inline                4.470000   0.000000   4.470000 (  4.490634)
separate              4.470000   0.000000   4.470000 (  4.480428)
out of the loop       4.450000   0.000000   4.450000 (  4.506627)
[robert@ora01 ~]$ cat rx.rb

require 'benchmark'

REP = 10_000
text = ("foo bar baz " * 1_000).freeze

Benchmark.bmbm 20 do |bm|
   bm.report "inline" do
     REP.times do
       text.scan(/bar/) do |match|
         match.length
       end
     end
   end

   bm.report "separate" do
     REP.times do
       rx = /bar/
       text.scan(rx) do |match|
         match.length
       end
     end
   end

   bm.report "out of the loop" do
     rx = /bar/
     REP.times do
       text.scan(rx) do |match|
         match.length
       end
     end
   end
end
[robert@ora01 ~]$

Cheers

	robert