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

Conversation

leoarnold
Copy link
Contributor

@leoarnold leoarnold commented Nov 13, 2021

The bigdecimal/util library adds #to_d to several numeric types, where

numeric.to_d(*args)

is equivalent to

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:

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

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@leoarnold leoarnold force-pushed the leoarnold/to_d branch 4 times, most recently from b13c0ca to b658961 Compare November 13, 2021 21:37
@leoarnold leoarnold force-pushed the leoarnold/to_d branch 2 times, most recently from bc5f1f2 to 5406db2 Compare November 16, 2021 02:57
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
```
@koic koic merged commit aa4208c into rubocop:master Nov 16, 2021
@koic
Copy link
Member

koic commented Nov 16, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants