From: Jason Roelofs Date: 2007-03-20T23:30:05+09:00 Subject: Re: Regexp and Prime numbers ------=_Part_151947_2317809.1174401001551 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 3/20/07, Tim Pease wrote: > > On 3/20/07, gga wrote: > > On Mar 19, 4:07 pm, "Tim Pease" wrote: > > > > > > ruby -wle 'puts "Prime" unless ("1" * ARGV[0].to_i) =~ > /^1$|^(11+?)\1+$/' 42 > > > > > > > This is pretty cool. Particularly as a simple benchmark for regex > > backtracking. > > It highlites some of the performance issues of backtracking in ruby1.8 > > and 1.9. > > And it also highlites issues with Perl's backtracking. > > > > Speed issues: > > > time ruby1.9 -wle 'puts "Prime" unless ("1" * ARGV[0].to_i) =~ > /^1$|^(11+?)\1+$/' 22441 > > Prime > > > > real 0m2.111s > > user 0m2.084s > > sys 0m0.024s > > > > > time perl -wle 'print "Prime" if (1 x shift) !~ /^1$|^(11+?)\1+$/' > 22441 > > Prime > > > > real 0m0.230s > > user 0m0.208s > > sys 0m0.020s > > > > > > But... on larger numbers... > > > > > ruby1.9 -wle 'puts "Prime" unless ("1" * ARGV[0].to_i) =~ > /^1$|^(11+?)\1+$/' 104729 > > Prime > > > > > perl -wle 'print "Prime" if (1 x shift) !~ /^1$|^(11+?)\1+$/' 104729 > > Segmentation fault > > > > > perl -v > > This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi > > > > Now that is very interesting. Do you know if Ruby 1.9 is running > Onigurma , the new regexp handler? > > For those of us on Windows command line: ruby -wle "puts 'Prime' unless ('1' * ARGV[0].to_i) =~ /^1$|^(11+?)\1+$/" That's cool. Jason ------=_Part_151947_2317809.1174401001551--