Skip to content

Instantly share code, notes, and snippets.

@badamrussell
Created January 10, 2014 03:05
Show Gist options
  • Save badamrussell/8346348 to your computer and use it in GitHub Desktop.
Save badamrussell/8346348 to your computer and use it in GitHub Desktop.
ruby testing
require 'benchmark'
Document = Struct.new(:id,:a,:b,:c)
documents_a = []
documents_h = {}
1.upto(10_000) do |n|
d = Document.new(n)
documents_a << d
documents_h[d.id] = d
end
searchlist = Array.new(1000){ rand(10_000)+1 }
Benchmark.bm(10) do |x|
x.report('array'){searchlist.each{|el| documents_a.any?{|d| d.id == el}} }
x.report('hash'){searchlist.each{|el| documents_h.has_key?(el)} }
end
# user system total real
#array 2.240000 0.020000 2.260000 ( 2.370452)
#hash 0.000000 0.000000 0.000000 ( 0.000695)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment