1.9.1 regex 6.5 times slower than 1.8.6 in at least one case

Discussion in 'Ruby' started by Michael Brooks, Feb 24, 2009.

  1. Hello:

    In general I've found Ruby 1.9.1 to be 2 times faster than 1.8.6 and
    roughly the same speed as Python 2.8 which is great... thanks guys,
    great job!

    However, in one case, when using a regular expressions (posted here by
    someone a long time ago) which I use to determine what numbers in
    0..10000 are prime numbers, version 1.9.1 was at least 6.5 times slower
    (1.8.6 = 67 secs, 1.9.1 = 457 secs).

    The regular express is:

    ((("1" * self) =~ /^1$|^(11+?)\1+$/) == 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) =~ /^1$|^(11+?)\1+$/) == nil)
    end
    end

    I also have a version of the prime-number calculation program that which
    doesn't use the above regex (i.e. it uses a traditional brute force
    approach instead) and it runs 2 times faster in 1.9.1. In 1.9.1 the
    regex gets exponentially worse as the number being evaluated exceeding
    1000.

    Does anyone know why the regex in 1.9.1 is so much slower than 1.8.6?

    Thank You,

    Michael
    Michael Brooks, Feb 24, 2009
    #1
    1. Advertising

  2. Michael Brooks wrote:
    > However, in one case, when using a regular expressions (posted here by
    > someone a long time ago) which I use to determine what numbers in
    > 0..10000 are prime numbers, version 1.9.1 was at least 6.5 times slower
    > (1.8.6 = 67 secs, 1.9.1 = 457 secs).
    >
    > The regular express is:
    >
    > ((("1" * self) =~ /^1$|^(11+?)\1+$/) == nil)


    As the only guy who would rather use a regex rather than string slicing,
    that's disheartening news. One thing you might want to check is your
    encoding. If your default encoding is UTF8, some string operations can
    be significantly slower:

    $ cat p-regex.rb
    4000.times do |i|
    ((("1" * i) =~ /^1$|^(11+?)\1+$/) == nil)
    end

    $ time 1.8/bin/ruby -KN -v p-regex.rb
    ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux]
    real 0m4.411s
    user 0m4.292s
    sys 0m0.032s

    $ time 1.8/bin/ruby -KU -v p-regex.rb
    ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux]
    real 0m4.480s
    user 0m4.320s
    sys 0m0.004s

    $ time 1.9/bin/ruby -KN -v p-regex.rb
    ruby 1.9.1p0 (2009-02-22 revision 22551) [i686-linux]
    real 0m8.041s
    user 0m7.980s
    sys 0m0.020s

    $ time 1.9/bin/ruby -KU -v p-regex.rb
    ruby 1.9.1p0 (2009-02-22 revision 22551) [i686-linux]
    real 0m21.709s
    user 0m20.913s
    sys 0m0.032s

    With ascii encoding, ruby1.9 is still slower than 1.8 but at least not
    six times slower.

    --
    Daniel
    Daniel DeLorme, Feb 24, 2009
    #2
    1. Advertising

  3. Daniel DeLorme wrote:
    > Michael Brooks wrote:
    >> However, in one case, when using a regular expressions (posted here by
    >> someone a long time ago) which I use to determine what numbers in
    >> 0..10000 are prime numbers, version 1.9.1 was at least 6.5 times slower
    >> (1.8.6 = 67 secs, 1.9.1 = 457 secs).
    >>
    >> The regular express is:
    >>
    >> ((("1" * self) =~ /^1$|^(11+?)\1+$/) == nil)

    >
    > As the only guy who would rather use a regex rather than string slicing,
    > that's disheartening news. One thing you might want to check is your
    > encoding. If your default encoding is UTF8, some string operations can
    > be significantly slower:
    >
    > $ cat p-regex.rb
    > 4000.times do |i|
    > ((("1" * i) =~ /^1$|^(11+?)\1+$/) == nil)
    > end
    >
    > $ time 1.8/bin/ruby -KN -v p-regex.rb
    > ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux] > real 0m4.411s
    >
    > $ time 1.8/bin/ruby -KU -v p-regex.rb
    > ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux] > real 0m4.480s
    >
    > $ time 1.9/bin/ruby -KN -v p-regex.rb
    > ruby 1.9.1p0 (2009-02-22 revision 22551) [i686-linux] > real 0m8.041s
    >
    > $ time 1.9/bin/ruby -KU -v p-regex.rb
    > ruby 1.9.1p0 (2009-02-22 revision 22551) [i686-linux] > real 0m21.709s
    >
    > With ascii encoding, ruby1.9 is still slower than 1.8 but at least not
    > six times slower.
    >
    > Daniel
    >


    Hello Daniel:

    Interesting, I didn't know about those switches.

    I ran my 10000 iteration prime number calc program using ruby 1.9.1 with
    no switch then with the two switches you identified:

    ruby "fprim v5.rb" > 477.47 seconds
    ruby -KN "fprim v5.rb" > 500.89 seconds
    ruby -KU "fprim v5.rb" > 1059.04 seconds

    Each test was run twice and the lower number from each reported above.
    I'm not sure why it's slower than the numbers I first reported.

    I'm running Windows XP (which I know is slower than Linux and the
    default Windows compiled binaries for 1.9.1 might be having some impact
    too) on an unplugged AMD 1.9 GHz based laptop (it runs at about 1.1 GHz
    when unplugged) with 2 GB ram. I'm not sure what my default ruby
    encoding is but it appears to be a bit faster than the -KN switch.

    To put those numbers in perspective, if I run your code on my unplugged
    laptop with the -KN switch it takes 37.07 seconds and without any switch
    36.85 seconds. If I change your code to use 10000 iterations and run it
    with the -KN switch it takes 477.36 seconds and without any switch
    500.44 seconds (which is strange because it's a flip of numbers in the
    fprim test).

    All I can say regarding regex performance and 1.9.1 is OUCH.

    Michael
    Michael Brooks, Feb 25, 2009
    #3
  4. Michael Brooks

    BIl Kleb Guest

    Michael Brooks wrote:
    >
    > However, in one case, when using a regular expressions (posted here by
    > someone a long time ago) which I use to determine what numbers in
    > 0..10000 are prime numbers, version 1.9.1 was at least 6.5 times slower
    > (1.8.6 = 67 secs, 1.9.1 = 457 secs).


    I found similar results lately, but the difference was more like a
    factor of 12!

    RUBY 1.8.6:

    $ cd FUN3D
    $ find . -name \*_c.\?90 -exec rm {} \;
    $ time ruby Ruby/complex_transformations_spike.rb
    [...]
    real 0m25.221s
    user 0m22.292s
    sys 0m0.759s

    $ ruby -v
    ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
    (as comes with OS X)

    RUBY 1.9.1:

    $ cd /usr/local/src
    $ wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz
    $ tar zxf ruby-1.9.1-p0.tar.gz
    $ cd ruby-1.9.1-p0
    $ ./configure --program-suffix=19 --enable-shared
    $ make
    $ sudo make install
    $ cd -

    $ time ruby19 Ruby/complex_transformations_spike.rb
    [...]
    real 5m18.188s
    user 4m54.253s
    sys 0m2.866s

    $ ruby19 -v
    ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin9.6.0]

    Regards,
    --
    Bil Kleb
    http://fun3d.larc.nasa.gov
    BIl Kleb, Feb 25, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Rivers

    VS.NET is 10 times slower than VB6

    John Rivers, Aug 23, 2005, in forum: ASP .Net
    Replies:
    91
    Views:
    3,127
    =?Utf-8?B?TWFuaWthbmRhbg==?=
    Nov 2, 2005
  2. croeltgen
    Replies:
    1
    Views:
    500
    Andrew Thompson
    Oct 25, 2004
  3. Replies:
    15
    Views:
    514
    Roy Harvey
    Oct 26, 2006
  4. Jack Steven
    Replies:
    2
    Views:
    434
    Chris Rebert
    Mar 9, 2009
  5. Philip Semanchuk

    Re: C-extension 2 times slower than exe

    Philip Semanchuk, Jun 23, 2009, in forum: Python
    Replies:
    3
    Views:
    408
Loading...

Share This Page