Skip to content

Instantly share code, notes, and snippets.

@jdfrens
Last active April 27, 2018 14:43
Show Gist options
  • Save jdfrens/338b1129507d8a255af14bf99c546b0e to your computer and use it in GitHub Desktop.
Save jdfrens/338b1129507d8a255af14bf99c546b0e to your computer and use it in GitHub Desktop.
require 'benchmark'
foo = [[8, nil], [8, 99]] * 10_000_000
Benchmark.bm(7) do |x|
x.report("to_i") do
foo.each { |(a, b)| [a, b.to_i].max }
end
x.report("compact") do
foo.each { |(a, b)| [a, b].compact.max }
end
end
Ruby 2.2.7:
user system total real
to_i 7.890000 0.020000 7.910000 ( 7.916156)
compact 7.870000 0.010000 7.880000 ( 7.890511)
Ruby 2.4.2:
user system total real
to_i 2.120000 0.000000 2.120000 ( 2.132754)
compact 3.930000 0.010000 3.940000 ( 3.937682)
@peterwmwong
Copy link

Looks like some good stuff coming with 2.6...

Ruby 2.6.0-dev

              user     system      total        real
to_i      1.724766   0.000444   1.725210 (  1.726089)
compact   3.728575   0.001039   3.729614 (  3.730519)

... for the sake of comparison: https://gist.github.com/peterwmwong/bc1b6bd56f613134b5bfb2d2962c39ae

disapear

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