Skip to content

Commit

Permalink
Add BigDecimal benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater committed Feb 21, 2019
1 parent 013fa34 commit af6c44a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Idioms

- [General](#general)
- [Array](#array)
- [BigDecimal](#bigdecimal)
- [Date](#date)
- [Enumerable](#enumerable)
- [Hash](#hash)
Expand Down Expand Up @@ -655,6 +656,34 @@ Comparison:
inject block: 14063.1 i/s - 1.35x slower
```

### BigDecimal

##### `BigDecimal('1')` vs `BigDecimal('1.0')` vs `'1'.to_d` vs `'1.0'.to_d` vs `BigDecimal(1)` vs `1.to_d` vs `BigDecimal(1.0, 2)` vs `1.0.to_d` [code](code/bigdecimal/string-vs-numeric.rb)

```
$ ruby -v code/bigdecimal/string-vs-numeric.rb
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
Calculating -------------------------------------
integer string new 2.497M (± 2.0%) i/s - 12.645M in 5.066385s
float string new 2.414M (± 2.0%) i/s - 12.182M in 5.048473s
integer string to_d 2.402M (± 1.8%) i/s - 12.010M in 5.001784s
float string to_d 2.318M (± 1.8%) i/s - 11.663M in 5.032429s
integer new 1.601M (± 1.2%) i/s - 8.022M in 5.010201s
integer to_d 1.563M (± 1.8%) i/s - 7.817M in 5.001726s
float new 517.635k (± 4.5%) i/s - 2.604M in 5.042605s
float to_d 529.413k (± 3.6%) i/s - 2.671M in 5.051477s
Comparison:
integer string new: 2496860.3 i/s
float string new: 2414122.8 i/s - same-ish: difference falls within error
integer string to_d: 2401929.3 i/s - 1.04x slower
float string to_d: 2318272.7 i/s - 1.08x slower
integer new: 1601402.4 i/s - 1.56x slower
integer to_d: 1563335.6 i/s - 1.60x slower
float to_d: 529412.7 i/s - 4.72x slower
float new: 517634.5 i/s - 4.82x slower
```

### Date

##### `Date.iso8601` vs `Date.parse` [code](code/date/iso8601-vs-parse.rb)
Expand Down
48 changes: 48 additions & 0 deletions code/bigdecimal/string-vs-numeric.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'benchmark/ips'
require 'bigdecimal'
require 'bigdecimal/util'

def fastest
BigDecimal('1')
end

def fastest2
BigDecimal('1.0')
end

def fast
'1'.to_d
end

def fast2
'1.0'.to_d
end

def slow
BigDecimal(1)
end

def slow2
1.to_d
end

def slowest
BigDecimal(1.0, 2)
end

def slowest2
1.0.to_d
end


Benchmark.ips do |x|
x.report('integer string new') { fastest }
x.report('float string new') { fastest2 }
x.report('integer string to_d') { fast }
x.report('float string to_d') { fast2 }
x.report('integer new') { slow }
x.report('integer to_d') { slow2 }
x.report('float new') { slowest }
x.report('float to_d') { slowest2 }
x.compare!
end

0 comments on commit af6c44a

Please sign in to comment.