Michael Brooks
2/25/2009 6:25:00 AM
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