Actions
Bug #17383
closed3.0 recursion memory|speed issues
Description
Testing 3.0.0-preview2 against 2.7.2 I see it's still not as performant to 2.7.2 for the following code.
Below is code from Rosettacode.org
https://rosettacode.org/wiki/Palindromic_gapful_numbers#Orders_of_Magnitude_Faster:_Direct_Generation_of_Numbers
This is run on an I7, 3.5GHz, 16GB, Linux 5.9.10 laptop.
Using rvm, 2.7.2 uses at max ~50% of memory worst case, but for 3.0.0pre2 it hits ~70%.
It's also faster on 2.7.2 than 3.0.0-pre2
class PalNo
def initialize(digit)
@digit, @l, @dd = digit, 3, 11*digit
end
def fN(n)
return [0,1,2,3,4,5,6,7,8,9] if n==1
return [0,11,22,33,44,55,66,77,88,99] if n==2
a=[]; [0,1,2,3,4,5,6,7,8,9].product(fN(n-2)).each{ |g| a << g[0]*10**(n-1)+g[0]+10*g[1] }; return a
end
def show(count, keep)
to_skip, palcnt, pals = count - keep, 0, []
while palcnt < count
fN(@l-2).each{ |g| pal=@digit*10**(@l-1)+@digit+10*g;
pals << pal if pal%(@dd)==0 && (palcnt += 1) > to_skip; break if palcnt - to_skip == keep }; @l+=1
end
print pals; puts
end
end
start = Time.now
(1..9).each { |digit| PalNo.new(digit).show(20, 20) }; puts "####"
(1..9).each { |digit| PalNo.new(digit).show(100, 15) }; puts "####"
(1..9).each { |digit| PalNo.new(digit).show(1000, 10) }; puts "####"
(1..9).each { |digit| PalNo.new(digit).show(100_000, 1) }; puts "####"
(1..9).each { |digit| PalNo.new(digit).show(1_000_000, 1) }; puts "####"
(1..9).each { |digit| PalNo.new(digit).show(10_000_000, 1) }; puts "####"
puts (Time.now - start)
Updated by marcandre (Marc-Andre Lafortune) almost 4 years ago
Thanks for reporting this.
Hopefully this related to #17373.
Updated by marcandre (Marc-Andre Lafortune) almost 4 years ago
- Related to Bug #17373: Ruby 3.0 is slower at Discourse bench than Ruby 2.7 added
Updated by jeremyevans0 (Jeremy Evans) about 1 year ago
- Status changed from Open to Closed
I tested the example:
- 2.7: 1178 seconds
- 3.2: 1315 seconds
- 3.2 --yjit: 1012 seconds
3.2 with --yjit is ~15% faster than 2.7, so whatever performance has been lost since 2.7 has been more than regained by --yjit.
Actions
Like0
Like0Like0Like0