Skip to content

Instantly share code, notes, and snippets.

@Insood

Insood/hash.rb Secret

Created September 17, 2018 18:44
Show Gist options
  • Save Insood/daf2a783e25657ee5ad3038715568b4b to your computer and use it in GitHub Desktop.
Save Insood/daf2a783e25657ee5ad3038715568b4b to your computer and use it in GitHub Desktop.
require 'benchmark'
array = []
1000000.times do
array << Random.rand
end
Benchmark.bm do |x|
x.report("Insert") do
h = {}
array.each do |a|
h[a] = true
end
end
x.report("Pass Array") do
a2 = array.map { |n| [n, true] }
h = Hash.new()
end
end
user system total real
Insert 0.240000 0.020000 0.260000 ( 0.235189)
Pass Array 0.140000 0.010000 0.150000 ( 0.135472)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment