-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 ```
- Loading branch information
Showing
5 changed files
with
116 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#269](https://github.com/rubocop/rubocop-performance/pull/269): Add `#to_d` support to `BigDecimalWithNumericArgument`. ([@leoarnold][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters