Created
May 19, 2016 15:42
-
-
Save repinel/6cf2e0668e57e1a80eebb534d683687a to your computer and use it in GitHub Desktop.
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 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'benchmark-ips' | |
end | |
Benchmark.ips do |x| | |
a = 10_000_000.times.map { |i| [i, i+2] } | |
x.report('Hash[Array]') do | |
Hash[a] | |
end | |
x.report('Array#to_h') do | |
a.to_h | |
end | |
x.compare! | |
end | |
# Warming up -------------------------------------- | |
# Hash[Array] 1.000 i/100ms | |
# Array#to_h 1.000 i/100ms | |
# Calculating ------------------------------------- | |
# Hash[Array] 0.181 (± 0.0%) i/s - 1.000 in 5.520885s | |
# Array#to_h 0.169 (± 0.0%) i/s - 1.000 in 5.934197s | |
# | |
# Comparison: | |
# Hash[Array]: 0.2 i/s | |
# Array#to_h: 0.2 i/s - 1.07x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment