Project

General

Profile

Actions

Bug #17383

closed

3.0 recursion memory|speed issues

Added by jzakiya (Jabari Zakiya) about 4 years ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:101346]

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)


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #17373: Ruby 3.0 is slower at Discourse bench than Ruby 2.7ClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0