Skip to content

Instantly share code, notes, and snippets.

@deepak
Last active December 16, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepak/5353921 to your computer and use it in GitHub Desktop.
Save deepak/5353921 to your computer and use it in GitHub Desktop.
Benchmark Integer, Float and BigDecimal
require 'benchmark'
require 'bigdecimal'
$CYCLE = 10_000_000
# Integer and Floats are native types
# BigDecimal is like a wrapper over a string
# Float is also a native type but is imprecise
# BigDecimal is precise and can handle arbitrary precision
# but is slower
# also check https://gist.github.com/deepak/1275050
Benchmark.bmbm do |x|
x.report "Integer" do
$CYCLE.times {|x| x + 1 }
end
x.report "Float" do
$CYCLE.times {|x| x + 0.0001 }
end
# string to BigDecimal is pretty common. eg. user input on a form
x.report "BigDecimal" do
$CYCLE.times {|x| BigDecimal.new("#{x}.0001") }
end
end
__END__
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin10.8.0]
Rehearsal ----------------------------------------------
Integer 0.690000 0.000000 0.690000 ( 0.690063)
Float 1.380000 0.000000 1.380000 ( 1.381334)
BigDecimal 15.160000 0.020000 15.180000 ( 15.182957)
------------------------------------ total: 17.250000sec
user system total real
Integer 0.730000 0.000000 0.730000 ( 0.722822)
Float 1.580000 0.000000 1.580000 ( 1.593262)
BigDecimal 17.590000 0.050000 17.640000 ( 17.685156)
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin10.8.0]
Rehearsal ----------------------------------------------
Integer 0.640000 0.000000 0.640000 ( 0.642389)
Float 0.910000 0.000000 0.910000 ( 0.908165)
BigDecimal 19.820000 0.030000 19.850000 ( 19.857600)
------------------------------------ total: 21.400000sec
user system total real
Integer 0.620000 0.000000 0.620000 ( 0.627930)
Float 0.920000 0.000000 0.920000 ( 0.923046)
BigDecimal 19.640000 0.020000 19.660000 ( 19.673304)
(on an older JDK and an older version of jruby)
jruby 1.7.1 (1.9.3p327) 2012-12-03 30a153b on Java HotSpot(TM) 64-Bit Server VM 1.6.0_43-b01-447-10M4203 [darwin-x86_64]
Rehearsal ----------------------------------------------
Integer 0.780000 0.030000 0.810000 ( 0.579000)
Float 0.760000 0.050000 0.810000 ( 0.666000)
BigDecimal 28.350000 1.240000 29.590000 ( 27.311000)
------------------------------------ total: 31.210000sec
user system total real
Integer 0.710000 0.040000 0.750000 ( 0.697000)
Float 0.770000 0.040000 0.810000 ( 0.756000)
BigDecimal 27.480000 1.250000 28.730000 ( 27.192000)
@anjalshireesh
Copy link

So, if the solution is to use Integer through Money, try to add a benchmark for Money as well and see what happens. Something like:

require 'benchmark'
require 'bigdecimal'
require 'money'

$CYCLE = 10_000_00

Benchmark.bmbm do |x|
  x.report "Integer" do
    $CYCLE.times {|x| amount = x + 1; currency = "INR" }
  end

  x.report "Float" do
    $CYCLE.times {|x| amount = x + 0.0001; currency = "INR" }
  end

  x.report "Money" do
    # multiply by 100, as we have to provide paise/cents to Money
    $CYCLE.times {|x| money = Money.new((x + 0.0001)*100, "INR") }
  end

  # string to BigDecimal is pretty common. eg. user input on a form
  x.report "BigDecimal" do
    $CYCLE.times {|x| amount = BigDecimal.new("#{x}.0001"); currency = "INR" }
  end
end

__END__

Rehearsal ----------------------------------------------
Integer      0.210000   0.000000   0.210000 (  0.210774)
Float        0.340000   0.000000   0.340000 (  0.342244)
Money       32.330000   0.000000  32.330000 ( 32.412949)
BigDecimal   2.010000   0.000000   2.010000 (  2.010116)
------------------------------------ total: 34.890000sec

                 user     system      total        real
Integer      0.230000   0.000000   0.230000 (  0.229390)
Float        0.370000   0.000000   0.370000 (  0.372463)
Money       32.390000   0.000000  32.390000 ( 32.471253)
BigDecimal   1.990000   0.000000   1.990000 (  1.998426)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment