Skip to content

Instantly share code, notes, and snippets.

@rubypanther
Created October 20, 2011 03:04
Show Gist options
  • Save rubypanther/1300313 to your computer and use it in GitHub Desktop.
Save rubypanther/1300313 to your computer and use it in GitHub Desktop.
Benchmark DecNumber vs Decimal vs BigDecimal vs Float
#!/usr/bin/env ruby
require 'decimal'
require 'bigdecimal'
require 'dec_number'
require 'benchmark'
include Benchmark
n = 123.58132134
m = Math::PI
bm(6) do |x|
x.report("dec_number") do
a = n.to_dec_number
b = m.to_dec_number
100_000.times do
((a * b + DecNumber.new(rand().to_s) - b / a) ** b).to_s
end
end
x.report("decimal") do
a = Decimal.new n.to_s
b = Decimal.new m.to_s
100_000.times do
((a * b + Decimal.new(rand().to_s) - b / a) ** b).to_s
end
end
x.report("bigdecimal") do
a = BigDecimal.new n.to_s
b = BigDecimal.new m.to_s
100_000.times do
(a * b + BigDecimal.new(rand().to_s) - b / a).to_s
end
end
x.report("float") do
a = n
b = m
100_000.times do
((a * b + rand() - b / a) ** b).to_s
end
end
end
user system total real
dec_number 2.640000 0.090000 2.730000 ( 2.819807)
decimal118.700000 1.450000 120.150000 (123.640275)
bigdecimal 1.730000 0.060000 1.790000 ( 1.845633)
float 0.310000 0.010000 0.320000 ( 0.333854)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment