Skip to content

Instantly share code, notes, and snippets.

@xxd
Created June 3, 2012 02:41
Show Gist options
  • Save xxd/2861051 to your computer and use it in GitHub Desktop.
Save xxd/2861051 to your computer and use it in GitHub Desktop.
NLvsHash.rb
def intersect1(a, b)
result = []
for x in a
for y in b
result.push(x) if x == y
end
end
return result
end
def intersect2(a, b)
hash = {}
result = []
for x in a
hash[x] = true
end
for y in b
return.push(y) if hash.key?(y)
end
return result
end
require 'benchmark'
a = (1..10000).collect{rand(1000)}
b = (1..10000).collect{rand(1000)}
Benchmark.bm do |x|
x.report {intersect1(a, b)}
x.report {intersect2(a, b)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment