Last active
December 16, 2015 01:19
-
-
Save deepak/5353921 to your computer and use it in GitHub Desktop.
Benchmark Integer, Float and 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' | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: