From 58b4b5120b24d0e2f7ba939da3c766f28a2e66ae Mon Sep 17 00:00:00 2001 From: Bart de Water Date: Wed, 20 Feb 2019 23:38:13 -0500 Subject: [PATCH] Add BigDecimal benchmarks --- README.md | 29 +++++++++++++++++ code/bigdecimal/string-vs-numeric.rb | 48 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 code/bigdecimal/string-vs-numeric.rb diff --git a/README.md b/README.md index 4924c4f..6313a23 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Idioms - [General](#general) - [Array](#array) +- [BigDecimal](#bigdecimal) - [Date](#date) - [Enumerable](#enumerable) - [Hash](#hash) @@ -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 to_d 529.413k (± 3.6%) i/s - 2.671M in 5.051477s + float new 517.635k (± 4.5%) i/s - 2.604M in 5.042605s + +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) diff --git a/code/bigdecimal/string-vs-numeric.rb b/code/bigdecimal/string-vs-numeric.rb new file mode 100644 index 0000000..5709558 --- /dev/null +++ b/code/bigdecimal/string-vs-numeric.rb @@ -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 to_d') { slowest2 } + x.report('float new') { slowest } + x.compare! +end