Skip to content

Instantly share code, notes, and snippets.

@AlexanderPavlenko
Created July 25, 2013 11:00
Show Gist options
  • Save AlexanderPavlenko/6078702 to your computer and use it in GitHub Desktop.
Save AlexanderPavlenko/6078702 to your computer and use it in GitHub Desktop.
values_at vs. map
require 'benchmark'
n = 1000
Benchmark.bm do |bm|
keys = (1..10000).to_a
hash = Hash[keys.zip(keys)]
bm.report do
n.times do
keys.map{|key| hash[key] }
end
end
bm.report do
n.times do
hash.values_at(*keys)
end
end
end
# ruby-2.0.0-p195
# user system total real
# 2.010000 0.060000 2.070000 ( 2.074412)
# 1.130000 0.050000 1.180000 ( 1.191744)
# jruby-1.7.4
# user system total real
# 1.680000 0.010000 1.690000 ( 1.148000)
# 0.450000 0.020000 0.470000 ( 0.410000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment