From: "Shot (Piotr Szotkowski)" Date: 2009-03-30T07:38:59+09:00 Subject: Re: 1.9.1 regex 6.5 times slower than 1.8.6 in at least one case --IS0zKkzwUGydFO0o Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable // I=E2=80=99m slowly trawling through my ruby-talk backlog, so apologies // if the below is irrelevant to anyone at this point in time. :) Michael Brooks: > However, in one case, when using a regular expressions (posted here by=20 > someone a long time ago) which I use to determine what numbers in=20 > 0..10000 are prime numbers, version 1.9.1 was at least 6.5 times=20 > slower (1.8.6 =3D 67 secs, 1.9.1 =3D 457 secs). > The regular express is: > ((("1" * self) =3D~ /^1$|^(11+?)\1+$/) =3D=3D nil) > which I've used in different versions of my program running either > on its own or as part of an overloading function as follows: > # Add an "is_prime?" method to the built-in numeric class > # which returns true if the number is a prime number. > class Fixnum > def is_prime? > ((("1" * self) =3D~ /^1$|^(11+?)\1+$/) =3D=3D nil) > end > end Note that this is a really ineffective way of testing primarity: ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux] >> class Fixnum >> def is_prime? >> ((("1" * self) =3D~ /^1$|^(11+?)\1+$/) =3D=3D nil) >> end >> end =3D> nil >> require 'prime' # adds Fixnum#prime? (among others) =3D> true >> start =3D Time.now; (0..1000).select(&:is_prime?); Time.now - start =3D> 0.405157579 >> start =3D Time.now; (0..1000).select(&:prime?); Time.now - start =3D> 0.016556705 >> start =3D Time.now; (0..5000).select(&:is_prime?); Time.now - start =3D> 30.165383712 >> start =3D Time.now; (0..5000).select(&:prime?); Time.now - start =3D> 0.105887884 Note that the Prime class has two bugs (as of 1.9.1-p0, both will be fixed in the next 1.9.1 release), but your regex also has one of them: >> 0.is_prime? =3D> true # BUG >> 1.is_prime? =3D> false >> 0.prime? =3D> true # BUG >> 1.prime? =3D> true # BUG (Neither 0 nor 1 are prime numbers.) > I also have a version of the prime-number calculation program that=20 > which doesn't use the above regex (i.e. it uses a traditional brute=20 > force approach instead) and it runs 2 times faster in 1.9.1. I highly recommend http://ruby-doc.org/core-1.9/ =E2=86=92 Prime class (et = al.). =E2=80=94 Shot --=20 >> The Ruby community should proceed with all deliberate >> speed towards ISO standardization of the language. > Yeah, look what it did to Forth. Don=E2=80=99t just say it, show it. http://vividpicture.com/aleks/atari/for= th.jpg [M. Edward (Ed) Borasky, Matt Lawrence, Gregory Brown, ruby-talk] --IS0zKkzwUGydFO0o Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAknP9kYACgkQi/mCfdEo8UpwAACfRdRlYmKafSNntNzvguDvzMME floAoJ+BO1HQ9wMWOCvqpCrkqbrXWz2X =PCnV -----END PGP SIGNATURE----- --IS0zKkzwUGydFO0o--