Last active
July 21, 2016 19:40
-
-
Save pgeraghty/380aa5bb0aeed1d1c45986356fabe9a9 to your computer and use it in GitHub Desktop.
Benchmark Rational vs. BigDecimal
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
require 'benchmark/ips' | |
require 'bigdecimal' | |
Benchmark.ips do |x| | |
x.report('Rational from String') { '214.04'.to_r - '74.79'.to_r + '74.79'.to_r > '214.04'.to_r } | |
x.report('Rational from Float') { 214.04.to_r - 74.79.to_r + 74.79.to_r > 214.04.to_r } | |
x.report('BigDecimal from String') { BigDecimal.new('214.04') - BigDecimal.new('74.79') + BigDecimal.new('74.79') > BigDecimal.new('214.04') } | |
x.report('BigDecimal from Float (precision auto)') { BigDecimal.new(214.04, 0) - BigDecimal.new(74.79, 0) + BigDecimal.new(74.79, 0) > BigDecimal.new(214.04, 0) } | |
x.report('BigDecimal from Float (precision 12)') { BigDecimal.new(214.04, 12) - BigDecimal.new(74.79, 12) + BigDecimal.new(74.79, 12) > BigDecimal.new(214.04, 12) } | |
x.compare! | |
end | |
=begin | |
Comparison: | |
Rational from Float: 414019.6 i/s | |
BigDecimal from String: 321991.7 i/s - 1.29x slower | |
Rational from String: 207141.2 i/s - 2.00x slower | |
BigDecimal from Float (precision 12): 90158.5 i/s - 4.59x slower | |
BigDecimal from Float (precision auto): 89520.5 i/s - 4.62x slower | |
=end | |
Benchmark.ips do |x| | |
x.report('BigDecimal from String comparison') { BigDecimal.new('0.161738278059140662') > BigDecimal.new('0.161738278059140663') } | |
x.report('BigDecimal from Float comparison') { BigDecimal.new(0.161738278059140662, 0) > BigDecimal.new(0.161738278059140663, 0) } | |
x.report('Rational from String comparison') { '0.161738278059140662'.to_r > '0.161738278059140663'.to_r } | |
x.report('Rational from Float comparison') { 0.161738278059140662.to_r > 0.161738278059140663.to_r } | |
x.report('Mixed comparison') { '0.161738278059140662'.to_r > BigDecimal.new('0.161738278059140663') } | |
x.compare! | |
end | |
=begin | |
Comparison: | |
Rational from Float comparison: 813176.0 i/s | |
BigDecimal from String comparison: 709671.6 i/s - 1.15x slower | |
Rational from String comparison: 327161.3 i/s - 2.49x slower | |
Mixed comparison: 231617.9 i/s - 3.51x slower | |
BigDecimal from Float comparison: 189118.7 i/s - 4.30x slower | |
=end | |
Benchmark.ips do |x| | |
rate_rational = '0.161738278059140662'.to_r | |
rate_bigdecimal = BigDecimal.new('0.161738278059140662') | |
x.report('Pre-parsed Rational vs. BigDecimal') { rate_rational > BigDecimal.new('0.161738278059140663') } | |
x.report('Pre-parsed Rational vs. Rational') { rate_rational > '0.161738278059140663'.to_r } | |
x.report('Pre-parsed BigDecimal vs. BigDecimal') { rate_bigdecimal > BigDecimal.new('0.161738278059140663') } | |
x.report('Pre-parsed BigDecimal vs. Rational') { rate_bigdecimal > '0.161738278059140663'.to_r } | |
x.compare! | |
end | |
=begin | |
Comparison: | |
Pre-parsed BigDecimal vs. BigDecimal: 1332389.2 i/s | |
Pre-parsed Rational vs. Rational: 565026.2 i/s - 2.36x slower | |
Pre-parsed Rational vs. BigDecimal: 354856.6 i/s - 3.75x slower | |
Pre-parsed BigDecimal vs. Rational: 314632.9 i/s - 4.23x slower | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment