Last active
August 29, 2015 14:07
-
-
Save lukaszkorecki/f0caa8f8030cfbc27d68 to your computer and use it in GitHub Desktop.
This file contains 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
bighash = {}.tap do |h| | |
(1..40_000).each do |i| | |
h[i.to_s] = i | |
end | |
end | |
smallhash = { "1" => 2, "5" => 6, "3" => 4 } | |
def keys_to_ints1 h | |
{}.tap do |x| | |
h.each do |k,v| | |
x[k.to_i] = v | |
end | |
end | |
end | |
def keys_to_ints2 h | |
k = h.keys.map(&:to_i) | |
Hash[k.zip(h.values)] | |
end | |
raise "f" if not keys_to_ints1(smallhash) == keys_to_ints2(smallhash) | |
require 'benchmark' | |
Benchmark.bmbm do |x| | |
x.report("keys_to_ints1") { keys_to_ints1(bighash) } | |
x.report("keys_to_ints2") { keys_to_ints2(bighash) } | |
end | |
=begin | |
Rehearsal ------------------------------------------------- | |
keys_to_ints1 0.030000 0.010000 0.040000 ( 0.027994) | |
keys_to_ints2 0.010000 0.000000 0.010000 ( 0.019940) | |
---------------------------------------- total: 0.050000sec | |
user system total real | |
keys_to_ints1 0.030000 0.000000 0.030000 ( 0.033963) | |
keys_to_ints2 0.020000 0.000000 0.020000 ( 0.020238) | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment