Skip to content

Instantly share code, notes, and snippets.

@workmad3
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save workmad3/ce2f87a79f1a682fc769 to your computer and use it in GitHub Desktop.

Select an option

Save workmad3/ce2f87a79f1a682fc769 to your computer and use it in GitHub Desktop.
require 'benchmark'
hsh = {}
1000.times {|i| hsh[i.to_s] = i.succ.to_s}
Benchmark.bm do |x|
x.report("Hash[]") {1000.times{Hash[hsh.values.zip(hsh.keys)]}}
x.report("invert") {1000.times{hsh.invert}}
x.report("each_with_object") {1000.times{hsh.each_with_object({}){|(k,v),h| h[v] = k}}}
end
user system total real
Hash[] 0.990000 0.050000 1.040000 ( 1.160492)
invert 0.810000 0.010000 0.820000 ( 0.882803)
each_with_object 1.180000 0.010000 1.190000 ( 1.354336)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment