Skip to content

Instantly share code, notes, and snippets.

@vidmantas
Last active December 11, 2015 15:08
Show Gist options
  • Save vidmantas/4618664 to your computer and use it in GitHub Desktop.
Save vidmantas/4618664 to your computer and use it in GitHub Desktop.
Some naive bigdecimal comparison
require 'bigdecimal'
require 'benchmark'
n = 100_000
Benchmark.bm do |x|
x.report("float") { n.times{ 15.0 / 13.2; 15.0 * 13.2; 15.0 - 13.2; 15.0 + 13.2 } }
x.report("bd on the fly") do
n.times do
BigDecimal.new("15.0") / BigDecimal.new("13.2")
BigDecimal.new("15.0") * BigDecimal.new("13.2")
BigDecimal.new("15.0") - BigDecimal.new("13.2")
BigDecimal.new("15.0") + BigDecimal.new("13.2")
end
end
x.report("bd cached objs") do
n.times do
fift = BigDecimal.new("15.0")
thirt = BigDecimal.new("13.2")
fift / thirt
fift * thirt
fift - thirt
fift + thirt
end
end
end
###################################################
user system total real
float 0.010000 0.000000 0.010000 ( 0.016775)
bd on the fly 0.680000 0.000000 0.680000 ( 0.676219)
bd cached objs 0.360000 0.000000 0.360000 ( 0.361639)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment