The Computer Language
24.12 Benchmarks Game

pidigits Ruby yjit #5 program

source code

# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
# contributed by Aaron Tavistock
# fixed by SO Alex

# Leverage GMP like all the other languages 
require_relative "gmp.so"

# Helpers that improve readability
class GMP::Z
  def mul!(a,b)
    GMP::Z.mul(self, a, b)
  end

  def times!(a)
    GMP::Z.mul(self, self, a)
  end
end

# Constants to reduce object instantiation and casting
ZERO = GMP::Z(0)
ONE = GMP::Z(1)
TWO = GMP::Z(2)
THREE = GMP::Z(3)
TEN = GMP::Z(10)

# Allocate these expensive objects once
@display_chunk = GMP::Z(0)
@k = GMP::Z(0)
@a = GMP::Z(0)
@t = GMP::Z(0)
@u = GMP::Z(0)
@k1 = GMP::Z(1)
@n = GMP::Z(1)
@d = GMP::Z(1)
@tmp = GMP::Z(0)

def next_chunk
  @tmp.mul!(@d, @t)
  @a.sub!(@tmp)
  @a.times!(TEN)
  @n.times!(TEN)
end

def produce_chunk
  @k.add!(ONE)
  @t.mul!(@n, TWO)
  @n.times!(@k)

  @a.add!(@t)
  @k1.add!(TWO)
  @a.times!(@k1)
  @d.times!(@k1)
  
  if @a >= @n
    @tmp.mul!(@n, THREE)
    @tmp.add!(@a)
    @t = @tmp.fdiv(@d)
    @u = @tmp.fmod(@d)
    @u.add!(@n)
    if @d > @u
      @display_chunk.times!(TEN)
      @display_chunk.add!(@t)
      return true
    end
  end
  
  false
end  

N = (ARGV[0] || 100).to_i
count = 0
while(count < N) do
  if produce_chunk
    count += 1
    if count % 10 == 0
      STDOUT.write "%010d\t:%d\n" % [@display_chunk.to_i, count]
      @display_chunk.times!(ZERO)
    end 
    next_chunk
  end
end

if @display_chunk.to_i > 0
  STDOUT.write "%s\t:%d\n" % [@display_chunk.to_s.ljust(10), count]
end
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
ruby 3.4.0dev
(2024-12-25
master f450108330)
+YJIT +PRISM [x86_64-linux]


 Thu, 26 Dec 2024 03:45:34 GMT

COMMAND LINE:
 /opt/src/ruby-3.4.0/bin/ruby --yjit -W0 pidigits.ruby-5.ruby 2000

PROGRAM FAILED 


PROGRAM OUTPUT:

pidigits.ruby-5.ruby:7:in 'Kernel#require_relative': /home/dunham/all-benchmarksgame/2000-benchmarksgame/bench/pidigits/gmp.so: undefined symbol: ruby_abi_version - ruby_abi_version (LoadError)
	from pidigits.ruby-5.ruby:7:in '<main>'