Created
June 3, 2012 02:41
-
-
Save xxd/2861051 to your computer and use it in GitHub Desktop.
NLvsHash.rb
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
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