On 02/02/2012 05:20 PM, Ryan Davis wrote:
> 
> On Feb 2, 2012, at 14:55 , Dmitry Nikiforov wrote:
> 
>> Here's another example with significantly bigger performance difference:
>>
>> Ruby:
>>
>> s = "This is a test string"
>>
>> re = Regexp.new( / test / )
>>
>> for a in 0..1E7
>>        re.match( s )
>> end
>>
>> Perl:
>>
>> my $s = "This is a test string";
>>
>> for my $a ( 0..1E7 ) {
>>       $s =~ / test /;
>> }
>>
>> Perl takes about 1.5 seconds to execute this, while Ruby takes a 
>> whopping 16!!! :((( I have a very strong feeling that I didn't compile 
>> Ruby properly - there can't be such a huge difference in regexp matching 
>> :(
> 
> It's all the parens, whitespace, and use of tabs that slows ruby down:

Ryan is being a little facetious about the parenthesis and whitespace in
case that isn't clear.  He has strong preferences about coding style. :-)

Your test above runs in about 10 seconds on my system under Ruby 1.9.2.
 The following equivalent code runs in about 6 seconds and is fairly
idiomatic Ruby:

s = "This is a test string"
(0..1E7).each do
  s =~ / test /
end

This code runs in about 4 seconds, but it is a bit less pretty to my eyes:

s = "This is a test string"
i = 0
while i < 1E7 do
  s =~ / test /
  i += 1
end

I'm sure there are other solutions as well.  The thing to keep in mind
is that method calls in Ruby are relatively expensive, so if you need
speed, you should try to avoid them.

Don't get hung up on micro benchmarks like the above though!  They can
really be deceiving with respect to real world applications.

-Jeremy