On Thu, 23 Sep 2004, Robert Klemme wrote:

>
> "ts" <decoux / moulon.inra.fr> schrieb im Newsbeitrag
> news:200409231451.i8NEphE08333 / moulon.inra.fr...
>>>>>>> "A" == Ara T Howard <Ara.T.Howard / noaa.gov> writes:
>>
>> A>    if s.strip.empty?
>> A>      # the string is whitespace only
>>
>> svg% ruby -e 'a = " \000\000"; p "OK" if a.strip.empty?'
>> "OK"
>> svg%
>>
>> svg% ruby -e 'a = " \000\000 "; p "OK" if a.strip.empty?'
>> svg%
>
> Also I'd say the disadvantage of "a.strip.empty?" is that it creates a copy
> of the string (=> a new instance) which is generally slower than a simple
> regexp check.

i assumed you were correct - but this is suprising:

   harp:~ > ruby b.rb
   -
   small string strip-empty:
     elapsed : 0.0081329345703125
   -
   small string re:
     elapsed : 0.005950927734375
   -
   small string re-precompiled:
     elapsed : 0.00719404220581055
   -
   big string strip-empty:
     elapsed : 0.263929843902588
   -
   big string re:
     elapsed : 5.26733493804932
   -
   big string re-precompiled:
     elapsed : 5.51002883911133

   harp:~ > cat b.rb
   $VERBOSE = nil
   STDOUT.sync = true

   def time label
     fork do
       GC.disable
       puts "-\n#{ label }:\n"
       a = Time::now.to_f
       yield
       b = Time::now.to_f
       puts "  elapsed : #{ b - a }"
     end
     Process::wait
   end

   s = "42"
   bs = s * 8192
   rep = %r/^\s*$/o


   time('small string strip-empty') do
     8192.times{ s.strip.empty? }
   end
   time('small string re') do
     8192.times{ s =~ %r/^\s*$/ }
   end
   time('small string re-precompiled') do
     8192.times{ s =~ rep }
   end
   time('big string strip-empty') do
     8192.times{ bs.strip.empty? }
   end
   time('big string re') do
     8192.times{ bs =~ %r/^\s*$/ }
   end
   time('big string re-precompiled') do
     8192.times{ bs =~ rep }
   end


at least it suprised me!

regards.


-a
--
===============================================================================
| EMAIL   :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE   :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it. 
|   --Dogen
===============================================================================