Skip to content

Conversation

@tompng
Copy link
Member

@tompng tompng commented Jul 17, 2025

Ruby's Integer implements faster algorithm (karatsuba, toom3, GMP)
Using Integer's div/mult needs base conversion, but it's still faster when precision is large.

# Mult threshold prec=150; x=BigDecimal('7'*9*prec); 10000.times{x*x} # processing time: 0.867585s prec=149; x=BigDecimal('7'*9*prec); 10000.times{x*x} # processing time: 1.041640s # Div threashold prec=299; y=BigDecimal('7'*9*prec); 10000.times { BigDecimal(1).div(y, 9*prec) } # processing time: 1.426509s prec=300; y=BigDecimal('7'*9*prec); 10000.times { BigDecimal(1).div(y, 9*prec) } # processing time: 1.299771s
# Performance degrade case. x = BigDecimal('1'*9*300); (x*7).div(x, 9*290) #=> 0.7e1 # processing time: 0.016612ms x = BigDecimal('1'*9*300); (x*7).div(x, 9*300) #=> 0.7e1 # processing time: 0.209515ms
…recision Ruby's Integer implements faster algorithm (karatsuba, toom3, GMP)
@tompng tompng force-pushed the large_mult_div_use_integer branch from 67fcd5d to 291ae1c Compare July 17, 2025 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant