From: Peter Vandenabeele Date: 2012-02-03T08:33:26+09:00 Subject: Re: Basic Ruby performance --485b397dce695c9d0004b8039f21 Content-Type: text/plain; charset=UTF-8 On Fri, Feb 3, 2012 at 12:20 AM, 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: > Euhmmm, I doubt that ... > > # takes 26.6 seconds on my laptop: > > s = "This is a test string" > > re = Regexp.new( / test / ) > > for a in 0..1E7 > re.match( s ) > end > > # takes 8.67 seconds on my laptop: > > s = "This is a test string" > > for a in 0..1E7 > s =~ / test / > end > The same "formatted" code with just replacing re.match( s) by s =~ /test/ also causes the same change from 22 to 7 seconds on my system (with the same formatting, spaces, etc.). I rather expect it's because `match` and `=~` do quite different things ... `match` returns a complete MatchData object `=~` returns the index (position) of the first match 017:0> re.match( s ) => # 018:0> s =~ /test/ => 10 Maybe (speculation) the MatchData object takes more dynamic Object allocation and thus more calls to the GC ? HTH, Peter --485b397dce695c9d0004b8039f21--