Last active
December 14, 2017 14:10
-
-
Save ndemianc/f5537021c8d2d1822db04f884760cbfd to your computer and use it in GitHub Desktop.
Benchmark Array#to_h and Enumerable#inject
This file contains hidden or 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' | |
array = Array.new(1_000_000, { id: 'key' }) | |
values = ['.p12', '.pfx'] | |
Benchmark.bmbm(7) do |bm| | |
bm.report('to_h') do | |
array.map do |prop| | |
[ | |
prop[:id], | |
values | |
] | |
end.to_h | |
end | |
bm.report('inject') do | |
array.inject({}) do |acc, prop| | |
acc.merge("#{prop[:id]}" => values) | |
end | |
end | |
end | |
Rehearsal ------------------------------------------- | |
to_h 0.400000 0.020000 0.420000 ( 0.435615) | |
inject 2.090000 0.110000 2.200000 ( 2.209907) | |
---------------------------------- total: 2.620000sec | |
user system total real | |
to_h 0.300000 0.160000 0.460000 ( 0.449012) | |
inject 1.980000 0.090000 2.070000 ( 2.075692) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment