Skip to content

Instantly share code, notes, and snippets.

@repinel
Created May 19, 2016 15:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save repinel/6cf2e0668e57e1a80eebb534d683687a to your computer and use it in GitHub Desktop.
Save repinel/6cf2e0668e57e1a80eebb534d683687a to your computer and use it in GitHub Desktop.
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