Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #to_d support to BigDecimalWithNumericArgument #269

Merged
merged 1 commit into from Nov 16, 2021

Commits on Nov 16, 2021

  1. Add #to_d support to BigDecimalWithNumericArgument

    The [`bigdecimal/util`](https://github.com/ruby/bigdecimal/blob/v3.0.2/lib/bigdecimal/util.rb)
    library adds `#to_d` to several numeric types, where
    
    ```ruby
    numeric.to_d(*args)
    ```
    
    is equivalent to
    
    ```ruby
    BigDecimal(numeric, *args)
    ```
    
    and the performance improvement of `BigDecimalWithNumericArgument`
    therefore equally applies here.
    
    Also, the exception of `Float` with specified precision does not seem
    to make sense and is therefore dropped:
    
    ```
    Warming up --------------------------------------
       BigDecimal("1.2")   266.340k i/100ms
    BigDecimal("1.2", 9)   261.542k i/100ms
      BigDecimal(1.2, 9)    44.366k i/100ms
                1.2.to_d    42.809k i/100ms
             1.2.to_d(9)    43.915k i/100ms
    Calculating -------------------------------------
       BigDecimal("1.2")      2.634M (± 1.8%) i/s -     13.317M in   5.056712s
    BigDecimal("1.2", 9)      2.510M (± 5.4%) i/s -     12.554M in   5.016970s
      BigDecimal(1.2, 9)    330.664k (±32.5%) i/s -      1.464M in   5.055963s
                1.2.to_d    183.891k (± 7.3%) i/s -    941.798k in   5.148850s
             1.2.to_d(9)    189.455k (± 9.4%) i/s -    966.130k in   5.149545s
    ```
    
    Generated with:
    
    ```ruby
    require 'bigdecimal'
    require 'bigdecimal/util'
    require 'benchmark/ips'
    
    numeric = 1.2
    string = numeric.to_s
    prec = rand(7..11)
    
    Benchmark.ips do |ips|
      ips.report("BigDecimal(\"#{string}\")") { BigDecimal(string) }
      ips.report("BigDecimal(\"#{string}\", #{prec})") { BigDecimal(string, prec) }
      ips.report("BigDecimal(#{numeric}, #{prec})") { BigDecimal(numeric, prec) }
      ips.report("#{numeric}.to_d") { numeric.to_d  }
      ips.report("#{numeric}.to_d(#{prec})") { numeric.to_d(prec) }
    end
    ```
    leoarnold committed Nov 16, 2021
    Configuration menu
    Copy the full SHA
    8651f14 View commit details
    Browse the repository at this point in the history